On 7/8/06, bradford <[EMAIL PROTECTED]> wrote:
>
> I have  a Model Foo, with a FK to user.
>
> if i do f = Foo.objects.get(id=1) and all i need from that is f.title
> and j.user.username in my template... is it returning more info than
> needed with User.  I mean, is it getting the entire user object for
> each returned record?
>
> If it is returning the entire user object, would it then be best to use
> just SQL and a JOIN?

If, instead, you're fetching lots of foo objects and need to get users
for all of them, you might want to use select_related, which does the
joins on the original fetch of Foo's, rather than making a separate
request per .user

http://www.djangoproject.com/documentation/db_api/#select-related

Caution, though, since select_related follows as far as it can, which
might be further than you actually want (say, if user then has a FK to
something else).  If that's the case, maybe you want extra(), which
would allow you to do a subquery for the user name.

http://www.djangoproject.com/documentation/db_api/#extra-select-none-where-none-params-none-tables-none

Something like:
Foo.objects.all().extra({'user_name':'select app_user.user_name from
app_user where app_user.id = app_foo.user'})

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to