On Fri, 2007-10-19 at 06:54 -0700, äL wrote:
> I would like to order a list in a view by foreign key. If I try
> ordering how in the
> code below my list is ordered by 'person'. And this means that the
> list
> ist ordered by the ID of the table Person. But I need a list ordered
> by 'nameLast'.
> Thus I changed ordering 'person' into 'person.nameLast'. But then I
> got an error
> "Column not found".
> 
> Does anybody have an ideia how to order by a foreign key? I spent a
> lot of
> time for searching any solution. Unfortunatelly without success.

Ordering by 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 at
http://www.djangoproject.com/documentation/db-api/#order-by-fields but
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-refactor/tests/regressiontests/queries/models.py#L290

[2]
http://code.djangoproject.com/browser/django/branches/queryset-refactor/tests/regressiontests/queries/models.py#L62

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