Skip to content

Commit 751d732

Browse files
authored
Fixed #35056 -- Fixed system check crash on reverse m2m relations with related_name in ModelAdmin.filter_horizontal/vertical.
Thanks Thomas Feldmann for the report. Regression in 1078657.
1 parent 21b0b23 commit 751d732

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

django/contrib/admin/checks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def _check_filter_item(self, obj, field_name, label):
532532
field=field_name, option=label, obj=obj, id="admin.E019"
533533
)
534534
else:
535-
if not field.many_to_many:
535+
if not field.many_to_many or isinstance(field, models.ManyToManyRel):
536536
return must_be(
537537
"a many-to-many field", option=label, obj=obj, id="admin.E020"
538538
)

docs/releases/5.0.1.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ Bugfixes
3232

3333
* Fixed a regression in Django 5.0 where querysets referenced incorrect field
3434
names from ``FilteredRelation()`` (:ticket:`35050`).
35+
36+
* Fixed a regression in Django 5.0 that caused a system check crash when
37+
``ModelAdmin.filter_horizontal`` or ``filter_vertical`` contained a reverse
38+
many-to-many relation with ``related_name`` (:ticket:`35056`).

tests/modeladmin/test_checks.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ class TestModelAdmin(ModelAdmin):
322322
"admin.E020",
323323
)
324324

325+
@isolate_apps("modeladmin")
326+
def test_invalid_reverse_m2m_field_with_related_name(self):
327+
class Contact(Model):
328+
pass
329+
330+
class Customer(Model):
331+
contacts = ManyToManyField("Contact", related_name="customers")
332+
333+
class TestModelAdmin(ModelAdmin):
334+
filter_vertical = ["customers"]
335+
336+
self.assertIsInvalid(
337+
TestModelAdmin,
338+
Contact,
339+
"The value of 'filter_vertical[0]' must be a many-to-many field.",
340+
"admin.E020",
341+
)
342+
325343
@isolate_apps("modeladmin")
326344
def test_invalid_m2m_field_with_through(self):
327345
class Artist(Model):
@@ -384,6 +402,24 @@ class TestModelAdmin(ModelAdmin):
384402
"admin.E020",
385403
)
386404

405+
@isolate_apps("modeladmin")
406+
def test_invalid_reverse_m2m_field_with_related_name(self):
407+
class Contact(Model):
408+
pass
409+
410+
class Customer(Model):
411+
contacts = ManyToManyField("Contact", related_name="customers")
412+
413+
class TestModelAdmin(ModelAdmin):
414+
filter_horizontal = ["customers"]
415+
416+
self.assertIsInvalid(
417+
TestModelAdmin,
418+
Contact,
419+
"The value of 'filter_horizontal[0]' must be a many-to-many field.",
420+
"admin.E020",
421+
)
422+
387423
@isolate_apps("modeladmin")
388424
def test_invalid_m2m_field_with_through(self):
389425
class Artist(Model):

0 commit comments

Comments
 (0)