mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Fixed #34513 -- Added system check for relational fields in ModelAdmin.list_display.
This commit is contained in:
parent
0e444e84f8
commit
c61219a7ae
@ -916,9 +916,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
|
||||
id="admin.E108",
|
||||
)
|
||||
]
|
||||
if isinstance(field, models.ManyToManyField) or (
|
||||
getattr(field, "rel", None) and field.rel.field.many_to_one
|
||||
):
|
||||
if (
|
||||
getattr(field, "is_relation", False)
|
||||
and (field.many_to_many or field.one_to_many)
|
||||
) or (getattr(field, "rel", None) and field.rel.field.many_to_one):
|
||||
return [
|
||||
checks.Error(
|
||||
f"The value of '{label}' must not be a many-to-many field or a "
|
||||
|
@ -554,6 +554,30 @@ class ListDisplayTests(CheckTestCase):
|
||||
"admin.E109",
|
||||
)
|
||||
|
||||
def test_invalid_related_field(self):
|
||||
class TestModelAdmin(ModelAdmin):
|
||||
list_display = ["song"]
|
||||
|
||||
self.assertIsInvalid(
|
||||
TestModelAdmin,
|
||||
Band,
|
||||
"The value of 'list_display[0]' must not be a many-to-many field or a "
|
||||
"reverse foreign key.",
|
||||
"admin.E109",
|
||||
)
|
||||
|
||||
def test_invalid_m2m_related_name(self):
|
||||
class TestModelAdmin(ModelAdmin):
|
||||
list_display = ["featured"]
|
||||
|
||||
self.assertIsInvalid(
|
||||
TestModelAdmin,
|
||||
Band,
|
||||
"The value of 'list_display[0]' must not be a many-to-many field or a "
|
||||
"reverse foreign key.",
|
||||
"admin.E109",
|
||||
)
|
||||
|
||||
def test_valid_case(self):
|
||||
@admin.display
|
||||
def a_callable(obj):
|
||||
|
Loading…
Reference in New Issue
Block a user