Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9916 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 415ffa8df56d72bf6f114abc8fdae11d459e768d 1 parent 4ad7725
Malcolm Tredinnick authored February 28, 2009
7  django/db/models/sql/query.py
@@ -716,7 +716,7 @@ def get_grouping(self):
716 716
         result, params = [], []
717 717
         if self.group_by is not None:
718 718
             group_by = self.group_by or []
719  
-            
  719
+
720 720
             extra_selects = []
721 721
             for extra_select, extra_params in self.extra_select.itervalues():
722 722
                 extra_selects.append(extra_select)
@@ -834,8 +834,9 @@ def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
834 834
             # the model.
835 835
             self.ref_alias(alias)
836 836
 
837  
-        # Must use left outer joins for nullable fields.
838  
-        self.promote_alias_chain(joins)
  837
+        # Must use left outer joins for nullable fields and their relations.
  838
+        self.promote_alias_chain(joins,
  839
+                self.alias_map[joins[0]][JOIN_TYPE] == self.LOUTER)
839 840
 
840 841
         # If we get to this point and the field is a relation to another model,
841 842
         # append the default ordering for that model.
31  tests/regressiontests/queries/models.py
@@ -238,6 +238,32 @@ class PointerA(models.Model):
238 238
 class PointerB(models.Model):
239 239
     connection = models.ForeignKey(SharedConnection)
240 240
 
  241
+# Multi-layer ordering
  242
+class SingleObject(models.Model):
  243
+    name = models.CharField(max_length=10)
  244
+
  245
+    class Meta:
  246
+        ordering = ['name']
  247
+
  248
+    def __unicode__(self):
  249
+        return self.name
  250
+
  251
+class RelatedObject(models.Model):
  252
+    single = models.ForeignKey(SingleObject)
  253
+
  254
+    class Meta:
  255
+        ordering = ['single']
  256
+
  257
+class Plaything(models.Model):
  258
+    name = models.CharField(max_length=10)
  259
+    others = models.ForeignKey(RelatedObject, null=True)
  260
+
  261
+    class Meta:
  262
+        ordering = ['others']
  263
+
  264
+    def __unicode__(self):
  265
+        return self.name
  266
+
241 267
 
242 268
 __test__ = {'API_TESTS':"""
243 269
 >>> t1 = Tag.objects.create(name='t1')
@@ -1044,6 +1070,11 @@ class PointerB(models.Model):
1044 1070
 >>> Annotation.objects.filter(notes__in=Note.objects.filter(note="n1").values_list('note').values('id'))
1045 1071
 [<Annotation: a1>]
1046 1072
 
  1073
+Bug #10028 -- ordering by model related to nullable relations(!) should use
  1074
+outer joins, so that all results are included.
  1075
+>>> _ = Plaything.objects.create(name="p1")
  1076
+>>> Plaything.objects.all()
  1077
+[<Plaything: p1>]
1047 1078
 """}
1048 1079
 
1049 1080
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__

0 notes on commit 415ffa8

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