On Fri, 2008-11-28 at 08:24 -0800, coan wrote:
[...]
> But isn't there a way to get all in one query?
> In my head the query would look something like this:
> user.edition_set.all().order_by(user__edition__rating)
Yes, there is a way to do it like that and I've been wondering why you
haven't already mentioned what you have tried here. The order_by()
documentation talks about how to order by related fields, so that's a
good place to start.
However, your proto-example is a little twisted around. user.edition_set
returns a queryset of Edition objects. So you refer to any other fields
as they are viewed from that model. It looks like
user.edition_set.order_by('rating__rating')
will do what you want.
Note, also, that I dropped the all() call. You only need all() if you
don't have any other queryset method (so that Django can differentiate
between the *manager* user.edition_set and the *queryset*
user.edition_set.all()). Since order_by() is also a method on managers
that returns a queryset, you don't need the all(). I only mention this
because I see example after example of people throwing around
unnecessary all() calls and I'd like to stamp out the plague. It doesn't
give incorrect examples, but it's a little less efficient and more
verbose than it has to be (the same as writing 'hello' + '' + ' ' +
'there' -- not wrong, but not useful, either).
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---