#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Type: Bug
Status: new | Component: Core
| (System checks)
Version: 5.1 | Severity: Normal
Keywords: check | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Bug was discussed in the forums here[https://forum.djangoproject.com/t
/manytomanyfield-error-message-with-incorrect-to-argument/39394].
Basically, if an invalid "to" argument is passed to ManyToManyField while
also passing the through/through_fields arguments the check framework
crashes giving unhelpful error message/traceback.
Minimal reproduceable example:
{{{
class Foo(Model):
pass
class Bar(Model):
foos = ManyToManyField(
to = "Fo", # NOTE incorrect "to" argument
through = 'FooBar',
through_fields = ('bar', 'foo')
)
class FooBar(Model):
foo = ForeignKey('Foo', on_delete=CASCADE)
bar = ForeignKey('Bar', on_delete=CASCADE)
}}}
Traceback:
{{{
File "django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/core/checks/model_checks.py", line 36, in check_all_models
errors.extend(model.check(**kwargs))
^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/base.py", line 1691, in check
*cls._check_fields(**kwargs),
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/base.py", line 1828, in _check_fields
errors.extend(field.check(from_model=cls, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/fields/related.py", line 1400, in check
*self._check_relationship_model(**kwargs),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "django/db/models/fields/related.py", line 1676, in
_check_relationship_model
related_model._meta.object_name,
^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_meta'
}}}
Proposed Patch:
{{{
diff --git a/django/db/models/fields/related.py
b/django/db/models/fields/related.py
index 6a9cb12a90..dd4c09a4e3 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -1707,13 +1707,18 @@ def _check_relationship_model(self,
from_model=None, **kwargs):
and getattr(field.remote_field, "model",
None)
== related_model
):
+ related_object_name = (
+ related_model
+ if isinstance(related_model, str)
+ else related_model._meta.object_name
+ )
errors.append(
checks.Error(
"'%s.%s' is not a foreign key to
'%s'."
% (
through._meta.object_name,
field_name,
- related_model._meta.object_name,
+ related_object_name,
),
hint=hint,
obj=self,
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36239>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701957b002cd2-70959e2f-c3f6-4328-828b-2b52172b7736-000000%40eu-central-1.amazonses.com.