Fixed #8291 -- Allowed 'pk' to be used as an ordering option in `Model.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
This commit is contained in:
parent
3db80c4c05
commit
d3154d1896
|
@ -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:
|
||||||
|
|
|
@ -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.
|
||||||
|
|
|
@ -24,3 +24,12 @@ class Article(models.Model):
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -5,7 +5,7 @@ from operator import attrgetter
|
||||||
|
|
||||||
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):
|
||||||
|
@ -137,3 +137,31 @@ class OrderingTests(TestCase):
|
||||||
],
|
],
|
||||||
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")
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in New Issue