mirror of
https://github.com/django/django.git
synced 2025-11-17 10:43:25 +00:00
Fix ordering check for related model pk in Meta.ordering
Allow 'pk' as an alias in Meta.ordering when traversing related fields to resolve regression from 440505cb2.
Adds tests for ordering by related model primary keys.
This commit is contained in:
parent
c2678e4975
commit
7fab1946c5
2 changed files with 32 additions and 1 deletions
|
|
@ -1708,6 +1708,10 @@ class Model(metaclass=ModelBase):
|
|||
fld = None
|
||||
for part in field.split(LOOKUP_SEP):
|
||||
try:
|
||||
# pk is an alias that won't be found by opts.get_field.
|
||||
if part == 'pk':
|
||||
fld = _cls._meta.pk
|
||||
else:
|
||||
fld = _cls._meta.get_field(part)
|
||||
if fld.is_relation:
|
||||
_cls = fld.get_path_info()[-1].to_opts.model
|
||||
|
|
|
|||
|
|
@ -836,6 +836,33 @@ class OtherModelTests(SimpleTestCase):
|
|||
|
||||
self.assertFalse(Child.check())
|
||||
|
||||
def test_ordering_pointing_to_related_model_pk(self):
|
||||
class Parent(models.Model):
|
||||
pass
|
||||
|
||||
class Child(models.Model):
|
||||
parent = models.ForeignKey(Parent, models.CASCADE)
|
||||
|
||||
class Meta:
|
||||
ordering = ('parent__pk',)
|
||||
|
||||
self.assertFalse(Child.check())
|
||||
|
||||
def test_ordering_pointing_to_two_related_model_pk(self):
|
||||
class Parent2(models.Model):
|
||||
pass
|
||||
|
||||
class Parent1(models.Model):
|
||||
parent2 = models.ForeignKey(Parent2, models.CASCADE)
|
||||
|
||||
class Child(models.Model):
|
||||
parent1 = models.ForeignKey(Parent1, models.CASCADE)
|
||||
|
||||
class Meta:
|
||||
ordering = ('parent1__parent2__pk',)
|
||||
|
||||
self.assertFalse(Child.check())
|
||||
|
||||
def test_name_beginning_with_underscore(self):
|
||||
class _Model(models.Model):
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue