Refs #29408 -- Cosmetic edits for validation of related fields and lookups in model Meta.ordering.

Follow up to 440505cb2c.
This commit is contained in:
Mariusz Felisiak 2019-03-01 19:38:21 +01:00 committed by GitHub
parent fe65918dca
commit f69c7bbdce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 12 deletions

View File

@ -1643,7 +1643,7 @@ class Model(metaclass=ModelBase):
# Convert "-field" to "field". # Convert "-field" to "field".
fields = ((f[1:] if f.startswith('-') else f) for f in fields) fields = ((f[1:] if f.startswith('-') else f) for f in fields)
# Separate related field and non related fields. # Separate related fields and non-related fields.
_fields = [] _fields = []
related_fields = [] related_fields = []
for f in fields: for f in fields:
@ -1667,7 +1667,7 @@ class Model(metaclass=ModelBase):
errors.append( errors.append(
checks.Error( checks.Error(
"'ordering' refers to the nonexistent field, " "'ordering' refers to the nonexistent field, "
"related field or lookup '%s'." % field, "related field, or lookup '%s'." % field,
obj=cls, obj=cls,
id='models.E015', id='models.E015',
) )
@ -1693,7 +1693,8 @@ class Model(metaclass=ModelBase):
for invalid_field in invalid_fields: for invalid_field in invalid_fields:
errors.append( errors.append(
checks.Error( checks.Error(
"'ordering' refers to the nonexistent field '%s'." % invalid_field, "'ordering' refers to the nonexistent field, related "
"field, or lookup '%s'." % invalid_field,
obj=cls, obj=cls,
id='models.E015', id='models.E015',
) )

View File

@ -277,7 +277,7 @@ Models
supported for that option. supported for that option.
* **models.E014**: ``ordering`` must be a tuple or list (even if you want to * **models.E014**: ``ordering`` must be a tuple or list (even if you want to
order by only one field). order by only one field).
* **models.E015**: ``ordering`` refers to the nonexistent field, related field * **models.E015**: ``ordering`` refers to the nonexistent field, related field,
or lookup ``<field name>``. or lookup ``<field name>``.
* **models.E016**: ``indexes/index_together/unique_together`` refers to field * **models.E016**: ``indexes/index_together/unique_together`` refers to field
``<field_name>`` which is not local to model ``<model>``. ``<field_name>`` which is not local to model ``<model>``.

View File

@ -632,7 +632,8 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"'ordering' refers to the nonexistent field 'relation'.", "'ordering' refers to the nonexistent field, related field, "
"or lookup 'relation'.",
obj=Model, obj=Model,
id='models.E015', id='models.E015',
), ),
@ -645,7 +646,8 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"'ordering' refers to the nonexistent field 'missing_field'.", "'ordering' refers to the nonexistent field, related field, "
"or lookup 'missing_field'.",
obj=Model, obj=Model,
id='models.E015', id='models.E015',
) )
@ -660,7 +662,8 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"'ordering' refers to the nonexistent field 'missing_fk_field_id'.", "'ordering' refers to the nonexistent field, related field, "
"or lookup 'missing_fk_field_id'.",
obj=Model, obj=Model,
id='models.E015', id='models.E015',
) )
@ -675,7 +678,7 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Model.check(), [ self.assertEqual(Model.check(), [
Error( Error(
"'ordering' refers to the nonexistent field, related field " "'ordering' refers to the nonexistent field, related field, "
"or lookup 'missing_related__id'.", "or lookup 'missing_related__id'.",
obj=Model, obj=Model,
id='models.E015', id='models.E015',
@ -694,7 +697,7 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Child.check(), [ self.assertEqual(Child.check(), [
Error( Error(
"'ordering' refers to the nonexistent field, related field " "'ordering' refers to the nonexistent field, related field, "
"or lookup 'parent__missing_field'.", "or lookup 'parent__missing_field'.",
obj=Child, obj=Child,
id='models.E015', id='models.E015',
@ -710,7 +713,7 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Child.check(), [ self.assertEqual(Child.check(), [
Error( Error(
"'ordering' refers to the nonexistent field, related field " "'ordering' refers to the nonexistent field, related field, "
"or lookup 'parent__missing_field'.", "or lookup 'parent__missing_field'.",
obj=Child, obj=Child,
id='models.E015', id='models.E015',
@ -732,7 +735,7 @@ class OtherModelTests(SimpleTestCase):
self.assertEqual(Child.check(), [ self.assertEqual(Child.check(), [
Error( Error(
"'ordering' refers to the nonexistent field, related field " "'ordering' refers to the nonexistent field, related field, "
"or lookup 'parent1__parent2__missing_field'.", "or lookup 'parent1__parent2__missing_field'.",
obj=Child, obj=Child,
id='models.E015', id='models.E015',

View File

@ -19,7 +19,7 @@ except ImportError:
pass pass
class TestModelMetaOrdering(PostgreSQLTestCase): class TestModelMetaOrdering(PostgreSQLSimpleTestCase):
def test_ordering_by_json_field_value(self): def test_ordering_by_json_field_value(self):
class TestJSONModel(JSONModel): class TestJSONModel(JSONModel):
class Meta: class Meta: