Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8291 -- Allowed 'pk' to be used as an ordering option in `Mode…

…l.Meta`. Thanks to peterd12 for the report and to evan_schulz, gruszczy, frog32 and David Gouldin for their work on the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17445 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d3154d1896c5cc1c046e43c2cf5253415d6429ae 1 parent 3db80c4
Julien Phalip authored February 04, 2012
5  django/core/management/validation.py
@@ -283,6 +283,11 @@ def get_validation_errors(outfile, app=None):
283 283
                 # this format would be nice, but it's a little fiddly).
284 284
                 if '__' in field_name:
285 285
                     continue
  286
+                # Skip ordering on pk, this is always a valid order_by field
  287
+                # but is an alias and therefore won't be found by
  288
+                # opts.get_field.
  289
+                if field_name == 'pk':
  290
+                    continue
286 291
                 try:
287 292
                     opts.get_field(field_name, many_to_many=False)
288 293
                 except models.FieldDoesNotExist:
9  tests/modeltests/invalid_models/invalid_models/models.py
@@ -233,6 +233,15 @@ class UnicodeForeignKeys(models.Model):
233 233
 class PrimaryKeyNull(models.Model):
234 234
     my_pk_field = models.IntegerField(primary_key=True, null=True)
235 235
 
  236
+class OrderByPKModel(models.Model):
  237
+    """
  238
+    Model to test that ordering by pk passes validation.
  239
+    Refs #8291
  240
+    """
  241
+    name = models.CharField(max_length=100, blank=True)
  242
+
  243
+    class Meta:
  244
+        ordering = ('pk',)
236 245
 
237 246
 model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer.
238 247
 invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
9  tests/modeltests/ordering/models.py
@@ -24,3 +24,12 @@ class Meta:
24 24
 
25 25
     def __unicode__(self):
26 26
         return self.headline
  27
+
  28
+class ArticlePKOrdering(models.Model):
  29
+    headline = models.CharField(max_length=100)
  30
+    pub_date = models.DateTimeField()
  31
+    class Meta:
  32
+        ordering = ('-pk',)
  33
+
  34
+    def __unicode__(self):
  35
+        return self.headline
30  tests/modeltests/ordering/tests.py
@@ -5,7 +5,7 @@
5 5
 
6 6
 from django.test import TestCase
7 7
 
8  
-from .models import Article
  8
+from .models import Article, ArticlePKOrdering
9 9
 
10 10
 
11 11
 class OrderingTests(TestCase):
@@ -137,3 +137,31 @@ def test_basic(self):
137 137
             ],
138 138
             attrgetter("headline")
139 139
         )
  140
+
  141
+    def test_order_by_pk(self):
  142
+        """
  143
+        Ensure that 'pk' works as an ordering option in Meta.
  144
+        Refs #8291.
  145
+        """
  146
+        a1 = ArticlePKOrdering.objects.create(
  147
+            pk=1, headline="Article 1", pub_date=datetime(2005, 7, 26)
  148
+        )
  149
+        a2 = ArticlePKOrdering.objects.create(
  150
+            pk=2, headline="Article 2", pub_date=datetime(2005, 7, 27)
  151
+        )
  152
+        a3 = ArticlePKOrdering.objects.create(
  153
+            pk=3, headline="Article 3", pub_date=datetime(2005, 7, 27)
  154
+        )
  155
+        a4 = ArticlePKOrdering.objects.create(
  156
+            pk=4, headline="Article 4", pub_date=datetime(2005, 7, 28)
  157
+        )
  158
+
  159
+        self.assertQuerysetEqual(
  160
+            ArticlePKOrdering.objects.all(), [
  161
+                "Article 4",
  162
+                "Article 3",
  163
+                "Article 2",
  164
+                "Article 1",
  165
+            ],
  166
+            attrgetter("headline")
  167
+        )

0 notes on commit d3154d1

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