Skip to content

Commit

Permalink
Fixed #8291 -- Allowed 'pk' to be used as an ordering option in `Mode…
Browse files Browse the repository at this point in the history
…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
jphalip committed Feb 4, 2012
1 parent 3db80c4 commit d3154d1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions django/core/management/validation.py
Expand Up @@ -283,6 +283,11 @@ def get_validation_errors(outfile, app=None):
# this format would be nice, but it's a little fiddly). # this format would be nice, but it's a little fiddly).
if '__' in field_name: if '__' in field_name:
continue 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: try:
opts.get_field(field_name, many_to_many=False) opts.get_field(field_name, many_to_many=False)
except models.FieldDoesNotExist: except models.FieldDoesNotExist:
Expand Down
9 changes: 9 additions & 0 deletions tests/modeltests/invalid_models/invalid_models/models.py
Expand Up @@ -233,6 +233,15 @@ class UnicodeForeignKeys(models.Model):
class PrimaryKeyNull(models.Model): class PrimaryKeyNull(models.Model):
my_pk_field = models.IntegerField(primary_key=True, null=True) 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. 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. invalid_models.fielderrors: "charfield2": CharFields require a "max_length" attribute that is a positive integer.
Expand Down
9 changes: 9 additions & 0 deletions tests/modeltests/ordering/models.py
Expand Up @@ -24,3 +24,12 @@ class Meta:


def __unicode__(self): def __unicode__(self):
return self.headline 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 changes: 29 additions & 1 deletion tests/modeltests/ordering/tests.py
Expand Up @@ -5,7 +5,7 @@


from django.test import TestCase from django.test import TestCase


from .models import Article from .models import Article, ArticlePKOrdering




class OrderingTests(TestCase): class OrderingTests(TestCase):
Expand Down Expand Up @@ -137,3 +137,31 @@ def test_basic(self):
], ],
attrgetter("headline") 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")
)

0 comments on commit d3154d1

Please sign in to comment.