Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
  • Loading branch information...
commit 106bd0f49ebbc0cf2d20a06082f552c71346fa0e 1 parent 7fdd26c
Malcolm Tredinnick authored
5  django/db/models/sql/query.py
@@ -687,8 +687,9 @@ def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
687 687
             # the model.
688 688
             self.ref_alias(alias)
689 689
 
690  
-        # Must use left outer joins for nullable fields.
691  
-        self.promote_alias_chain(joins)
  690
+        # Must use left outer joins for nullable fields and their relations.
  691
+        self.promote_alias_chain(joins,
  692
+                self.alias_map[joins[0]][JOIN_TYPE] == self.LOUTER)
692 693
 
693 694
         # If we get to this point and the field is a relation to another model,
694 695
         # append the default ordering for that model.
26  tests/regressiontests/queries/models.py
@@ -237,6 +237,32 @@ class PointerA(models.Model):
237 237
 class PointerB(models.Model):
238 238
     connection = models.ForeignKey(SharedConnection)
239 239
 
  240
+# Multi-layer ordering
  241
+class SingleObject(models.Model):
  242
+    name = models.CharField(max_length=10)
  243
+
  244
+    class Meta:
  245
+        ordering = ['name']
  246
+
  247
+    def __unicode__(self):
  248
+        return self.name
  249
+
  250
+class RelatedObject(models.Model):
  251
+    single = models.ForeignKey(SingleObject)
  252
+
  253
+    class Meta:
  254
+        ordering = ['single']
  255
+
  256
+class Plaything(models.Model):
  257
+    name = models.CharField(max_length=10)
  258
+    others = models.ForeignKey(RelatedObject, null=True)
  259
+
  260
+    class Meta:
  261
+        ordering = ['others']
  262
+
  263
+    def __unicode__(self):
  264
+        return self.name
  265
+
240 266
 
241 267
 __test__ = {'API_TESTS':"""
242 268
 >>> t1 = Tag.objects.create(name='t1')

0 notes on commit 106bd0f

Please sign in to comment.
Something went wrong with that request. Please try again.