From 106bd0f49ebbc0cf2d20a06082f552c71346fa0e Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 28 Feb 2009 03:02:52 +0000 Subject: [PATCH] [1.0.X] Fixed #10028 -- Fixed a problem when ordering by related models. Some results were inadvertently being excluded if we were ordering across a nullable relation which itself ordering by a non-nullable relation. Backport of r9916 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9917 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 5 +++-- tests/regressiontests/queries/models.py | 26 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 1d4001bab0..bb4ce92f43 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -687,8 +687,9 @@ class BaseQuery(object): # the model. self.ref_alias(alias) - # Must use left outer joins for nullable fields. - self.promote_alias_chain(joins) + # Must use left outer joins for nullable fields and their relations. + self.promote_alias_chain(joins, + self.alias_map[joins[0]][JOIN_TYPE] == self.LOUTER) # If we get to this point and the field is a relation to another model, # append the default ordering for that model. diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 50d81479a5..eb4db0cd19 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -237,6 +237,32 @@ class PointerA(models.Model): class PointerB(models.Model): connection = models.ForeignKey(SharedConnection) +# Multi-layer ordering +class SingleObject(models.Model): + name = models.CharField(max_length=10) + + class Meta: + ordering = ['name'] + + def __unicode__(self): + return self.name + +class RelatedObject(models.Model): + single = models.ForeignKey(SingleObject) + + class Meta: + ordering = ['single'] + +class Plaything(models.Model): + name = models.CharField(max_length=10) + others = models.ForeignKey(RelatedObject, null=True) + + class Meta: + ordering = ['others'] + + def __unicode__(self): + return self.name + __test__ = {'API_TESTS':""" >>> t1 = Tag.objects.create(name='t1')