From c813fb327cb1b09542be89c5ceed367826236bc2 Mon Sep 17 00:00:00 2001 From: Bakdolot <80908236+Bakdolot@users.noreply.github.com> Date: Mon, 24 Apr 2023 17:14:35 +0600 Subject: [PATCH] Fixed #34481 -- Added system check for reverse related fields in ModelAdmin.list_display. --- django/contrib/admin/checks.py | 7 +++++-- docs/ref/checks.txt | 4 ++-- tests/modeladmin/test_checks.py | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 27537d9614..0fefe65e48 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -916,10 +916,13 @@ class ModelAdminChecks(BaseModelAdminChecks): id="admin.E108", ) ] - if isinstance(field, models.ManyToManyField): + if isinstance(field, models.ManyToManyField) or ( + getattr(field, "rel", None) and field.rel.field.many_to_one + ): return [ checks.Error( - "The value of '%s' must not be a ManyToManyField." % label, + f"The value of '{label}' must not be a many-to-many field or a " + f"reverse foreign key.", obj=obj.__class__, id="admin.E109", ) diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index fa7c633487..9e350a3ff3 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -703,8 +703,8 @@ with the admin site: * **admin.E108**: The value of ``list_display[n]`` refers to ``