[3.0.x] Refs #30798 -- Prevented chaining fields from the same related model multiple times in model Meta.ordering.

Backport of c7944628a1 from master
This commit is contained in:
Hasan Ramezani 2019-09-26 10:33:54 +02:00 committed by Mariusz Felisiak
parent 311177d5d1
commit da8f85aa82
2 changed files with 22 additions and 0 deletions

View File

@ -1711,6 +1711,8 @@ class Model(metaclass=ModelBase):
fld = _cls._meta.get_field(part) fld = _cls._meta.get_field(part)
if fld.is_relation: if fld.is_relation:
_cls = fld.get_path_info()[-1].to_opts.model _cls = fld.get_path_info()[-1].to_opts.model
else:
_cls = None
except (FieldDoesNotExist, AttributeError): except (FieldDoesNotExist, AttributeError):
if fld is None or fld.get_transform(part) is None: if fld is None or fld.get_transform(part) is None:
errors.append( errors.append(

View File

@ -814,6 +814,26 @@ class OtherModelTests(SimpleTestCase):
) )
]) ])
def test_ordering_pointing_multiple_times_to_model_fields(self):
class Parent(models.Model):
field1 = models.CharField(max_length=100)
field2 = models.CharField(max_length=100)
class Child(models.Model):
parent = models.ForeignKey(Parent, models.CASCADE)
class Meta:
ordering = ('parent__field1__field2',)
self.assertEqual(Child.check(), [
Error(
"'ordering' refers to the nonexistent field, related field, "
"or lookup 'parent__field1__field2'.",
obj=Child,
id='models.E015',
)
])
def test_ordering_allows_registered_lookups(self): def test_ordering_allows_registered_lookups(self):
class Model(models.Model): class Model(models.Model):
test = models.CharField(max_length=100) test = models.CharField(max_length=100)