Fixed #34481 -- Added system check for reverse related fields in ModelAdmin.list_display.

This commit is contained in:
Bakdolot 2023-04-24 17:14:35 +06:00 committed by GitHub
parent 83c9765f45
commit c813fb327c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -916,10 +916,13 @@ class ModelAdminChecks(BaseModelAdminChecks):
id="admin.E108", 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 [ return [
checks.Error( 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__, obj=obj.__class__,
id="admin.E109", id="admin.E109",
) )

View File

@ -703,8 +703,8 @@ with the admin site:
* **admin.E108**: The value of ``list_display[n]`` refers to ``<label>``, * **admin.E108**: The value of ``list_display[n]`` refers to ``<label>``,
which is not a callable, an attribute of ``<ModelAdmin class>``, or an which is not a callable, an attribute of ``<ModelAdmin class>``, or an
attribute or method on ``<model>``. attribute or method on ``<model>``.
* **admin.E109**: The value of ``list_display[n]`` must not be a * **admin.E109**: The value of ``list_display[n]`` must not be a many-to-many
``ManyToManyField`` field. field or a reverse foreign key.
* **admin.E110**: The value of ``list_display_links`` must be a list, a tuple, * **admin.E110**: The value of ``list_display_links`` must be a list, a tuple,
or ``None``. or ``None``.
* **admin.E111**: The value of ``list_display_links[n]`` refers to ``<label>``, * **admin.E111**: The value of ``list_display_links[n]`` refers to ``<label>``,

View File

@ -537,7 +537,20 @@ class ListDisplayTests(CheckTestCase):
self.assertIsInvalid( self.assertIsInvalid(
TestModelAdmin, TestModelAdmin,
ValidationTestModel, ValidationTestModel,
"The value of 'list_display[0]' must not be a ManyToManyField.", "The value of 'list_display[0]' must not be a many-to-many field or a "
"reverse foreign key.",
"admin.E109",
)
def test_invalid_reverse_related_field(self):
class TestModelAdmin(ModelAdmin):
list_display = ["song_set"]
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", "admin.E109",
) )