Thanks for the tip--APN.zip was a ForeignKey with null=True. The SQL queries helped me verify. Appreciate it!
-----Original Message----- From: django-users@googlegroups.com [mailto:[EMAIL PROTECTED] Behalf Of Rajesh Dhawan Sent: Wednesday, February 06, 2008 1:56 PM To: Django users Subject: Re: sorting by another table On Feb 6, 3:50 pm, <[EMAIL PROTECTED]> wrote: > I have a Sale model that foreign keys into an APN model, which foreign keys > into a Zip model. I'd like to sort my Sale objects by the 'zip' field on the > Zip model, but > > Sale.objects.select_related().order_by('sales_zip.zip') > > doesn't work (1054, "Unknown column 'sales_zip.zip' in 'order clause'"). It > only works if I sort by something on the APN model, which is one foreign-key > hop instead of two. It may be that the Zip table is not getting joined in when Django constructs the SQL query. For a start, you should go into debug mode, and print out the query that Django is creating. See: http://www.djangoproject.com/documentation/faq/#how-can-i-see-the-raw-sql-queries-django-is-running Perhaps Django is bringing in the table "sales_zip" under a different name. I've had cases where it would bring such a table in under the alias sales_zip2, etc. for good reason.s Now, the use of "select_related()" should join in that table, but may be APN.zip is a nullable ForeignKey? -Rajesh D --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---