1
0
mirror of https://github.com/django/django.git synced 2025-02-01 13:19:18 +00:00

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 107865780aa44914e21d27fdf4ca269bc61c7f01.
This commit is contained in:
Mariusz Felisiak 2023-12-27 20:36:22 +01:00 committed by GitHub
parent 21b0b23a67
commit 751d732a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 1 deletions

View File

@ -532,7 +532,7 @@ class BaseModelAdminChecks:
field=field_name, option=label, obj=obj, id="admin.E019"
)
else:
if not field.many_to_many:
if not field.many_to_many or isinstance(field, models.ManyToManyRel):
return must_be(
"a many-to-many field", option=label, obj=obj, id="admin.E020"
)

View File

@ -32,3 +32,7 @@ Bugfixes
* Fixed a regression in Django 5.0 where querysets referenced incorrect field
names from ``FilteredRelation()`` (:ticket:`35050`).
* Fixed a regression in Django 5.0 that caused a system check crash when
``ModelAdmin.filter_horizontal`` or ``filter_vertical`` contained a reverse
many-to-many relation with ``related_name`` (:ticket:`35056`).

View File

@ -322,6 +322,24 @@ class FilterVerticalCheckTests(CheckTestCase):
"admin.E020",
)
@isolate_apps("modeladmin")
def test_invalid_reverse_m2m_field_with_related_name(self):
class Contact(Model):
pass
class Customer(Model):
contacts = ManyToManyField("Contact", related_name="customers")
class TestModelAdmin(ModelAdmin):
filter_vertical = ["customers"]
self.assertIsInvalid(
TestModelAdmin,
Contact,
"The value of 'filter_vertical[0]' must be a many-to-many field.",
"admin.E020",
)
@isolate_apps("modeladmin")
def test_invalid_m2m_field_with_through(self):
class Artist(Model):
@ -384,6 +402,24 @@ class FilterHorizontalCheckTests(CheckTestCase):
"admin.E020",
)
@isolate_apps("modeladmin")
def test_invalid_reverse_m2m_field_with_related_name(self):
class Contact(Model):
pass
class Customer(Model):
contacts = ManyToManyField("Contact", related_name="customers")
class TestModelAdmin(ModelAdmin):
filter_horizontal = ["customers"]
self.assertIsInvalid(
TestModelAdmin,
Contact,
"The value of 'filter_horizontal[0]' must be a many-to-many field.",
"admin.E020",
)
@isolate_apps("modeladmin")
def test_invalid_m2m_field_with_through(self):
class Artist(Model):