[1.7.x] Fixed #22711 -- Adjusted ordering checks to allow implicit relation fields.
refs #19195.
Backport of d04e730224
from master
This commit is contained in:
parent
33511662dd
commit
d773a08b27
|
@ -1341,7 +1341,7 @@ class Model(six.with_metaclass(ModelBase)):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _check_ordering(cls):
|
def _check_ordering(cls):
|
||||||
""" Check "ordering" option -- is it a list of lists and do all fields
|
""" Check "ordering" option -- is it a list of strings and do all fields
|
||||||
exist? """
|
exist? """
|
||||||
|
|
||||||
from django.db.models import FieldDoesNotExist
|
from django.db.models import FieldDoesNotExist
|
||||||
|
@ -1385,6 +1385,14 @@ class Model(six.with_metaclass(ModelBase)):
|
||||||
try:
|
try:
|
||||||
cls._meta.get_field(field_name, many_to_many=False)
|
cls._meta.get_field(field_name, many_to_many=False)
|
||||||
except FieldDoesNotExist:
|
except FieldDoesNotExist:
|
||||||
|
if field_name.endswith('_id'):
|
||||||
|
try:
|
||||||
|
field = cls._meta.get_field(field_name[:-3], many_to_many=False)
|
||||||
|
except FieldDoesNotExist:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if field.attname == field_name:
|
||||||
|
continue
|
||||||
errors.append(
|
errors.append(
|
||||||
checks.Error(
|
checks.Error(
|
||||||
"'ordering' refers to the non-existent field '%s'." % field_name,
|
"'ordering' refers to the non-existent field '%s'." % field_name,
|
||||||
|
|
|
@ -391,6 +391,40 @@ class OtherModelTests(IsolatedModelsTestCase):
|
||||||
]
|
]
|
||||||
self.assertEqual(errors, expected)
|
self.assertEqual(errors, expected)
|
||||||
|
|
||||||
|
def test_ordering_pointing_to_missing_foreignkey_field(self):
|
||||||
|
# refs #22711
|
||||||
|
|
||||||
|
class Model(models.Model):
|
||||||
|
missing_fk_field = models.IntegerField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ("missing_fk_field_id",)
|
||||||
|
|
||||||
|
errors = Model.check()
|
||||||
|
expected = [
|
||||||
|
Error(
|
||||||
|
"'ordering' refers to the non-existent field 'missing_fk_field_id'.",
|
||||||
|
hint=None,
|
||||||
|
obj=Model,
|
||||||
|
id='models.E015',
|
||||||
|
)
|
||||||
|
]
|
||||||
|
self.assertEqual(errors, expected)
|
||||||
|
|
||||||
|
def test_ordering_pointing_to_existing_foreignkey_field(self):
|
||||||
|
# refs #22711
|
||||||
|
|
||||||
|
class Parent(models.Model):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Child(models.Model):
|
||||||
|
parent = models.ForeignKey(Parent)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ("parent_id",)
|
||||||
|
|
||||||
|
self.assertFalse(Child.check())
|
||||||
|
|
||||||
@override_settings(TEST_SWAPPED_MODEL_BAD_VALUE='not-a-model')
|
@override_settings(TEST_SWAPPED_MODEL_BAD_VALUE='not-a-model')
|
||||||
def test_swappable_missing_app_name(self):
|
def test_swappable_missing_app_name(self):
|
||||||
class Model(models.Model):
|
class Model(models.Model):
|
||||||
|
|
Loading…
Reference in New Issue