Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 jphalip authored
5 django/core/management/validation.py
View
@@ -283,6 +283,11 @@ def get_validation_errors(outfile, app=None):
# this format would be nice, but it's a little fiddly).
if '__' in field_name:
continue
+ # Skip ordering on pk, this is always a valid order_by field
+ # but is an alias and therefore won't be found by
+ # opts.get_field.
+ if field_name == 'pk':
+ continue
try:
opts.get_field(field_name, many_to_many=False)
except models.FieldDoesNotExist:
9 tests/modeltests/invalid_models/invalid_models/models.py
View
@@ -233,6 +233,15 @@ class UnicodeForeignKeys(models.Model):
class PrimaryKeyNull(models.Model):
my_pk_field = models.IntegerField(primary_key=True, null=True)
+class OrderByPKModel(models.Model):
+ """
+ Model to test that ordering by pk passes validation.
+ Refs #8291
+ """
+ name = models.CharField(max_length=100, blank=True)
+
+ class Meta:
+ ordering = ('pk',)
model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute that is a positive integer.
invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
9 tests/modeltests/ordering/models.py
View
@@ -24,3 +24,12 @@ class Meta:
def __unicode__(self):
return self.headline
+
+class ArticlePKOrdering(models.Model):
+ headline = models.CharField(max_length=100)
+ pub_date = models.DateTimeField()
+ class Meta:
+ ordering = ('-pk',)
+
+ def __unicode__(self):
+ return self.headline
30 tests/modeltests/ordering/tests.py
View
@@ -5,7 +5,7 @@
from django.test import TestCase
-from .models import Article
+from .models import Article, ArticlePKOrdering
class OrderingTests(TestCase):
@@ -137,3 +137,31 @@ def test_basic(self):
],
attrgetter("headline")
)
+
+ def test_order_by_pk(self):
+ """
+ Ensure that 'pk' works as an ordering option in Meta.
+ Refs #8291.
+ """
+ a1 = ArticlePKOrdering.objects.create(
+ pk=1, headline="Article 1", pub_date=datetime(2005, 7, 26)
+ )
+ a2 = ArticlePKOrdering.objects.create(
+ pk=2, headline="Article 2", pub_date=datetime(2005, 7, 27)
+ )
+ a3 = ArticlePKOrdering.objects.create(
+ pk=3, headline="Article 3", pub_date=datetime(2005, 7, 27)
+ )
+ a4 = ArticlePKOrdering.objects.create(
+ pk=4, headline="Article 4", pub_date=datetime(2005, 7, 28)
+ )
+
+ self.assertQuerysetEqual(
+ ArticlePKOrdering.objects.all(), [
+ "Article 4",
+ "Article 3",
+ "Article 2",
+ "Article 1",
+ ],
+ attrgetter("headline")
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.