diff --git a/django/core/management/validation.py b/django/core/management/validation.py index 3aaeaa94c8..f0b5b1c7c2 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -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: diff --git a/tests/modeltests/invalid_models/invalid_models/models.py b/tests/modeltests/invalid_models/invalid_models/models.py index 93c1c66f1c..ed69fb60ee 100644 --- a/tests/modeltests/invalid_models/invalid_models/models.py +++ b/tests/modeltests/invalid_models/invalid_models/models.py @@ -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. diff --git a/tests/modeltests/ordering/models.py b/tests/modeltests/ordering/models.py index 25d3c2c90f..bfb4b97107 100644 --- a/tests/modeltests/ordering/models.py +++ b/tests/modeltests/ordering/models.py @@ -24,3 +24,12 @@ class Article(models.Model): 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 diff --git a/tests/modeltests/ordering/tests.py b/tests/modeltests/ordering/tests.py index 6a988b8398..b1b5253682 100644 --- a/tests/modeltests/ordering/tests.py +++ b/tests/modeltests/ordering/tests.py @@ -5,7 +5,7 @@ from operator import attrgetter from django.test import TestCase -from .models import Article +from .models import Article, ArticlePKOrdering class OrderingTests(TestCase): @@ -137,3 +137,31 @@ class OrderingTests(TestCase): ], 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") + )