Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #22711 -- Adjusted ordering checks to allow implicit relation f…

…ields.

refs #19195.
  • Loading branch information...
commit d04e7302240f5be34cdd303002bc8e7dcd81f529 1 parent 7a38f88
@a1tus a1tus authored charettes committed
View
10 django/db/models/base.py
@@ -1357,7 +1357,7 @@ def _check_local_fields(cls, fields, option):
@classmethod
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? """
from django.db.models import FieldDoesNotExist
@@ -1401,6 +1401,14 @@ def _check_ordering(cls):
try:
cls._meta.get_field(field_name, many_to_many=False)
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(
checks.Error(
"'ordering' refers to the non-existent field '%s'." % field_name,
View
34 tests/invalid_models_tests/test_models.py
@@ -415,6 +415,40 @@ class Meta:
]
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')
def test_swappable_missing_app_name(self):
class Model(models.Model):
Please sign in to comment.
Something went wrong with that request. Please try again.