Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7371 -- Fixed an edge case when ordering on related models.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7786 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 050d0a1b75add2240760c4ba09f1fdd6e9a677f0 1 parent c17e326
Malcolm Tredinnick authored June 29, 2008
5  django/db/models/sql/query.py
@@ -626,6 +626,11 @@ def find_ordering_name(self, name, opts, alias=None, default_order='ASC',
626 626
                 alias, False)
627 627
         alias = joins[-1]
628 628
         col = target.column
  629
+        if not field.rel:
  630
+            # To avoid inadvertent trimming of a necessary alias, use the
  631
+            # refcount to show that we are referencing a non-relation field on
  632
+            # the model.
  633
+            self.ref_alias(alias)
629 634
 
630 635
         # Must use left outer joins for nullable fields.
631 636
         for join in joins:
15  tests/regressiontests/queries/models.py
@@ -172,6 +172,17 @@ class Child(models.Model):
172 172
     person = models.OneToOneField(Member, primary_key=True)
173 173
     parent = models.ForeignKey(Member, related_name="children")
174 174
 
  175
+# Custom primary keys interfered with ordering in the past.
  176
+class CustomPk(models.Model):
  177
+    name = models.CharField(max_length=10, primary_key=True)
  178
+    extra = models.CharField(max_length=10)
  179
+
  180
+    class Meta:
  181
+        ordering = ['name', 'extra']
  182
+
  183
+class Related(models.Model):
  184
+    custom = models.ForeignKey(CustomPk)
  185
+
175 186
 
176 187
 __test__ = {'API_TESTS':"""
177 188
 >>> t1 = Tag.objects.create(name='t1')
@@ -786,5 +797,9 @@ class Child(models.Model):
786 797
 >>> n1.annotation_set.filter(Q(tag=t5) | Q(tag__children=t5) | Q(tag__children__children=t5))
787 798
 [<Annotation: a1>]
788 799
 
  800
+Bug #7371
  801
+>>> Related.objects.order_by('custom')
  802
+[]
  803
+
789 804
 """}
790 805
 

0 notes on commit 050d0a1

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