Thanks Malcolm, this helped me on one problem I had.  But, I've
another similar problem but in my case, I've multiple foreign keys, so
I'm not sure what to do about the database table name part.

My models:
class Member(models.Model):
  # first_name, last_name, etc.
  class Meta:
        ordering = ['first_name', 'last_name']

class Team(model.Model):
  foreman = model.ForeignKey(Member)
  operator = model.ForeignKey(Member)
  # and more ForeignKey(Member) fields
  class Meta:
        ordering = ['foreman',]


In the admin interface this orders the Teams by the first_name,
last_name of the foreman, which is what I want.  But in my view, when
I try to get a list of Teams using:
 all_teams = Team.objects.all()

the result isn't sorted by the foreman's [first_name, last_name].  I
don't know what it's sorted by, it's not id either.

I also tried:
 all_teams = Teams.objects.all().order_by('foreman')    # returns the
same as above, again not sorted.

[1] all_teams =
Teams.objects.all().order_by('appname_member.first_name')  # returns
error on eval, unknown column 'appname_member.first_name'.

The order_by(tablename.field) worked in another model with only one
reference to the foreign key (so, thanks for that tip, I missed it in
the docs.)  I didn't expect [1] to work, but don't know how to specify
that I want the sorting based on the foreman's first_name, last_name.
Any suggestions?  Is this possible using order_by()?  I can do it by
hand if not, but it'd be cleaner and simpler if I don't have to.

Many thanks.
Michael


On Oct 19, 7:11 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:

> Orderingby related fields is a bit crufty at the moment and it doesn't
> always work (e.g. ticket #2076). The general idea is that you have to
> use the database table name(!) followed by a dot followed by the target
> model field name (not the column name). This is actually documented 
> athttp://www.djangoproject.com/documentation/db-api/#order-by-fieldsbut
> you have to read it carefully.
>
> The good news is that this will shortly change so that you will be able
> to write person__nameLast in your case, just like you do for filters.
> That code is currently in the queryset-refactor branch and will be
> merged with trunk as soon as a few more slightly tricky problems with
> queries are ironed out.
>
> By way of example and as a sneak preview, [1] and [2] (in the Meta
> class) show the new syntax (that's a new test file on that branch).
>
> [1]http://code.djangoproject.com/browser/django/branches/queryset-refact...
>
> [2]http://code.djangoproject.com/browser/django/branches/queryset-refact...
>
> Regards,
> Malcolm
>
> --
> Remember that you are unique. Just like everyone 
> else.http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to