diff options
author | Bakdolot <80908236+Bakdolot@users.noreply.github.com> | 2023-05-02 10:46:22 +0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-02 06:46:22 +0200 |
commit | c61219a7ae051d2baab53f041e00592011fc550c (patch) | |
tree | 27397957fb68ebed1d24c7d8a6e48693faa76c30 | |
parent | 0e444e84f87d174713a2aef0c4f9704ce2865586 (diff) | |
download | django-c61219a7ae051d2baab53f041e00592011fc550c.tar.gz |
Fixed #34513 -- Added system check for relational fields in ModelAdmin.list_display.
-rw-r--r-- | django/contrib/admin/checks.py | 7 | ||||
-rw-r--r-- | tests/modeladmin/test_checks.py | 24 |
2 files changed, 28 insertions, 3 deletions
diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 0fefe65e48..c395b0bde2 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -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 " diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index 85f2eda69e..69aa242a64 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -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): |