Skip to content

Commit

Permalink
[3.0.x] Fixed #30798 -- Fixed Meta.ordering validation for pk of rela…
Browse files Browse the repository at this point in the history
…ted fields.

Regression in 440505c.

Backport of 95a1157 from master
  • Loading branch information
hramezani authored and felixxm committed Sep 27, 2019
1 parent da8f85a commit 0dc39ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,11 @@ def _check_ordering(cls):
fld = None
for part in field.split(LOOKUP_SEP):
try:
fld = _cls._meta.get_field(part)
# 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
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/invalid_models_tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,18 @@ class Meta:
with register_lookup(models.CharField, Lower):
self.assertEqual(Model.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.assertEqual(Child.check(), [])

def test_ordering_pointing_to_foreignkey_field(self):
class Parent(models.Model):
pass
Expand Down

0 comments on commit 0dc39ca

Please sign in to comment.