[Django] #36239: ManyToManyField check error with invalid "to" when passing through/through_fields

59 views
Skip to first unread message

Django

unread,
Mar 9, 2025, 9:02:50 AM3/9/25
to django-...@googlegroups.com
#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.

Django

unread,
Mar 9, 2025, 4:37:18 PM3/9/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner: (none)
Type: Bug | Status: new
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Amaan-ali03):

Hey Django Team,
I’d love to work on this issue!
Requesting to assign this ticket to @Amaan-ali03 to implement the proposed
fix for the ManyToManyField error with an invalid "to" argument.

Proposed Approach:

Identify the Issue: The bug occurs when an invalid "to" argument is passed
to ManyToManyField along with through and through_fields, causing an
AttributeError due to improper handling of the related_model variable.
Proposed Solution: Update the _check_relationship_model method in
django/db/models/fields/related.py to include a type check for
related_model. Instead of directly accessing related_model._meta, the code
should verify if related_model is a string and handle it accordingly to
avoid the crash.
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:1>

Django

unread,
Mar 9, 2025, 8:30:45 PM3/9/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| usmanasifbutt
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by usmanasifbutt):

* owner: (none) => usmanasifbutt
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:2>

Django

unread,
Mar 10, 2025, 3:19:11 AM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner: Usman
| Asif
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:3>

Django

unread,
Mar 10, 2025, 3:21:43 AM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner: Usman
| Asif
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:4>

Django

unread,
Mar 10, 2025, 12:04:33 PM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
--------------------------------------+------------------------------------
Reporter: Jordan Hyatt | Owner: (none)
Type: Bug | Status: new
Component: Core (System checks) | Version: 5.1
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Usman Asif):

* owner: Usman Asif => (none)
* status: assigned => new

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:5>

Django

unread,
Mar 10, 2025, 5:43:19 PM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner: Samruddhi
| Dharankar
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Samruddhi Dharankar):

* owner: (none) => Samruddhi Dharankar
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:6>

Django

unread,
Mar 10, 2025, 5:44:22 PM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
--------------------------------------+------------------------------------
Reporter: Jordan Hyatt | Owner: (none)
Type: Bug | Status: new
Component: Core (System checks) | Version: 5.1
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Samruddhi Dharankar):

* owner: Samruddhi Dharankar => (none)
* status: assigned => new

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:7>

Django

unread,
Mar 10, 2025, 7:32:14 PM3/10/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
--------------------------------------+------------------------------------
Reporter: Jordan Hyatt | Owner: (none)
Type: Bug | Status: new
Component: Core (System checks) | Version: 5.1
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Ahmed Nassar):

Hi Django Team,
I noticed that PR #19248 is currently open for this issue, but some
unresolved test failures remain.
Is the current contributor still working on it, or would it be okay for me
to assist in fixing the remaining issues?
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:8>

Django

unread,
Mar 12, 2025, 4:37:15 AM3/12/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by JaeHyuckSa):

* owner: (none) => JaeHyuckSa
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:9>

Django

unread,
Mar 21, 2025, 12:30:08 PM3/21/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tanishq):

Hey Django Team,

I’d love to jump on ticket #36239 (ManyToManyField crash with invalid to).
Can you assign it to @Amaan-ali03? I’ve dug into the issue and want to
implement the fix.

Issue:
An invalid to (e.g., "Fo") with t**hrough/through_fields** crashes with
**AttributeError**: 'str' has no '_meta' because r**elated_model** isn’t
handled properly.

Proposed Fix:
Update **_check_relationship_model in django/db/models/fields/related.py**
with a type check:


{{{
related_object_name = (
related_model if isinstance(related_model, str) else
related_model._meta.object_name
)
errors.append(
checks.Error(
f"'{through._meta.object_name}.{field_name}' is not a foreign key
to '{related_object_name}'.",
obj=self,
)
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:10>

Django

unread,
Mar 23, 2025, 10:53:05 AM3/23/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tanishq):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:11>

Django

unread,
Mar 23, 2025, 10:56:33 AM3/23/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tanishq):

Fixes #36239: Prevents `AttributeError: 'str' object has no attribute
'_meta'` in `ManyToManyField` when `to` is invalid with
`through`/`through_fields`.

- **Issue**: The validation framework assumed `related_model` was always
resolved, causing a crash when it remained a string.
- **Fix**: If `related_model` is a string, we handle it gracefully in
`_check_relationship_model`.
- **Test**: Added test in `tests/model_fields/tests.py` to confirm
expected error messages (`E300`, `E339`).
- **Impact**: Prevents crashes and ensures Django correctly raises
validation errors instead.


https://github.com/django/django/pull/19301
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:12>

Django

unread,
Mar 23, 2025, 11:36:15 AM3/23/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

We appreciate your interest Tanishq but there's no need to step in here.

JaeHyuckSa's [https://github.com/django/django/pull/19255/ PR is already
passing the full suite with any failures] and is only awaiting a final
review.
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:13>

Django

unread,
Mar 23, 2025, 12:33:42 PM3/23/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tanishq):

Replying to [comment:13 Simon Charette]:
> We appreciate your interest Tanishq but there's no need to step in here.
>
> JaeHyuckSa's [https://github.com/django/django/pull/19255/ PR is already
passing the full suite with any failures] and is only awaiting a final
review.

okay, thanks for informing me, I will continue contributing in other
issues, if that's fine?
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:14>

Django

unread,
Mar 23, 2025, 3:03:18 PM3/23/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

sure, try to find tickets that don't already have any active owner and
ready for review PRs.
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:15>

Django

unread,
Mar 27, 2025, 10:26:35 AM3/27/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: assigned
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution:
Keywords: check | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:16>

Django

unread,
Mar 28, 2025, 7:38:08 AM3/28/25
to django-...@googlegroups.com
#36239: ManyToManyField check error with invalid "to" when passing
through/through_fields
-------------------------------------+-------------------------------------
Reporter: Jordan Hyatt | Owner:
| JaeHyuckSa
Type: Bug | Status: closed
Component: Core (System | Version: 5.1
checks) |
Severity: Normal | Resolution: fixed
Keywords: check | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"c1a4fccf53437e480c42648f67277fb7e63ed4ab" c1a4fcc]:
{{{#!CommitTicketReference repository=""
revision="c1a4fccf53437e480c42648f67277fb7e63ed4ab"
Fixed #36239 -- Fixed a crash in ManyToManyField.through_fields check when
to model is invalid.

Signed-off-by: saJaeHyukc <wogur...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36239#comment:17>
Reply all
Reply to author
Forward
0 new messages