1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #26827 -- Improved ModelState error message when relations refer model classes.

This commit is contained in:
Prashant Pandey 2023-11-23 10:29:52 +05:30 committed by Mariusz Felisiak
parent a89c715c3b
commit a8adb6aa6c
3 changed files with 11 additions and 8 deletions

View File

@ -816,6 +816,7 @@ answer newbie questions, and generally made Django that much better:
plisk plisk
polpak@yahoo.com polpak@yahoo.com
pradeep.gowda@gmail.com pradeep.gowda@gmail.com
Prashant Pandey <https://prashantpandey9.in>
Preston Holmes <preston@ptone.com> Preston Holmes <preston@ptone.com>
Preston Timmons <prestontimmons@gmail.com> Preston Timmons <prestontimmons@gmail.com>
Priyank Panchal <priyankpanchal872000@gmail.com> Priyank Panchal <priyankpanchal872000@gmail.com>

View File

@ -738,13 +738,15 @@ class ModelState:
# Sanity-check that relation fields are NOT referring to a model class. # Sanity-check that relation fields are NOT referring to a model class.
if field.is_relation and hasattr(field.related_model, "_meta"): if field.is_relation and hasattr(field.related_model, "_meta"):
raise ValueError( raise ValueError(
'ModelState.fields cannot refer to a model class - "%s.to" does. ' 'Model fields in "ModelState.fields" cannot refer to a model class '
"Use a string reference instead." % name f'- "{self.app_label}.{self.name}.{name}.to" does. Use a string '
"reference instead."
) )
if field.many_to_many and hasattr(field.remote_field.through, "_meta"): if field.many_to_many and hasattr(field.remote_field.through, "_meta"):
raise ValueError( raise ValueError(
'ModelState.fields cannot refer to a model class - "%s.through" ' 'Model fields in "ModelState.fields" cannot refer to a model class '
"does. Use a string reference instead." % name f'- "{self.app_label}.{self.name}.{name}.through" does. Use a '
"string reference instead."
) )
# Sanity-check that indexes have their name set. # Sanity-check that indexes have their name set.
for index in self.options["indexes"]: for index in self.options["indexes"]:

View File

@ -1651,8 +1651,8 @@ class ModelStateTests(SimpleTestCase):
field = models.ForeignKey(UnicodeModel, models.CASCADE) field = models.ForeignKey(UnicodeModel, models.CASCADE)
with self.assertRaisesMessage( with self.assertRaisesMessage(
ValueError, ValueError,
'ModelState.fields cannot refer to a model class - "field.to" does. ' 'Model fields in "ModelState.fields" cannot refer to a model class - '
"Use a string reference instead.", '"app.Model.field.to" does. Use a string reference instead.',
): ):
ModelState("app", "Model", [("field", field)]) ModelState("app", "Model", [("field", field)])
@ -1661,8 +1661,8 @@ class ModelStateTests(SimpleTestCase):
field.remote_field.through = UnicodeModel field.remote_field.through = UnicodeModel
with self.assertRaisesMessage( with self.assertRaisesMessage(
ValueError, ValueError,
'ModelState.fields cannot refer to a model class - "field.through" does. ' 'Model fields in "ModelState.fields" cannot refer to a model class - '
"Use a string reference instead.", '"app.Model.field.through" does. Use a string reference instead.',
): ):
ModelState("app", "Model", [("field", field)]) ModelState("app", "Model", [("field", field)])