As per my comment on django-users:
If you have a query set which has had distinct() called and then order_by() on a foreign key
field you don't get the ordering on that foreign key field - the
resulting generated sql query has the joins for the foreign table
ready to be used for the order_by, but the "if not distinct or elt in
select_aliases" bit inside get_ordering() prevents the actual ORDER BY
portion being added.
The following example case illustrates. The models are:
from django.db import models
class FKTest(models.Model):
name = models.CharField()
class Test(models.Model):
fk = models.ForeignKey('FKTest')
name = models.CharField()
Using those models:
print str(Test.objects.all().order_by('fk__name').query)
print str(Test.objects.all().order_by('fk__name').distinct().query)
print str(Test.objects.all().order_by('name').query)
print str(Test.objects.all().order_by('name').distinct().query)
This gives the output (with psycopg being my database driver):
SELECT "crs_test"."id", "crs_test"."fk_id", "crs_test"."name" FROM "crs_test" INNER JOIN "crs_fktest" ON ("crs_test"."fk_id" = "crs_fktest"."id") ORDER BY "crs_fktest"."name" ASC
SELECT DISTINCT "crs_test"."id", "crs_test"."fk_id", "crs_test"."name" FROM "crs_test" INNER JOIN "crs_fktest" ON ("crs_test"."fk_id" = "crs_fktest"."id")
SELECT "crs_test"."id", "crs_test"."fk_id", "crs_test"."name" FROM "crs_test" ORDER BY "crs_test"."name" ASC
SELECT DISTINCT "crs_test"."id", "crs_test"."fk_id", "crs_test"."name" FROM "crs_test" ORDER BY "crs_test"."name" ASC
As you can see the DISTINCT version of the sort on a foreign object's name is missing the ORDER BY clause.