﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36239	"ManyToManyField check error with invalid ""to"" when passing through/through_fields"	Jordan Hyatt	JaeHyuckSa	"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,
}}}


"	Bug	closed	Core (System checks)	5.1	Normal	fixed	check	Jordan Hyatt	Ready for checkin	1	0	0	0	1	0
