Refs #2259 -- Disallowed primary keys in ModelAdmin.list_editable.

Refs #32728.
This commit is contained in:
siddhartha-star-dev 2022-02-18 23:04:05 +05:30 committed by Mariusz Felisiak
parent ed0a2c3238
commit dcebc5da48
2 changed files with 18 additions and 1 deletions

View File

@ -1130,7 +1130,7 @@ class ModelAdminChecks(BaseModelAdminChecks):
id="admin.E124", id="admin.E124",
) )
] ]
elif not field.editable: elif not field.editable or field.primary_key:
return [ return [
checks.Error( checks.Error(
"The value of '%s' refers to '%s', which is not editable " "The value of '%s' refers to '%s', which is not editable "

View File

@ -363,6 +363,23 @@ class SystemChecksTestCase(SimpleTestCase):
] ]
self.assertEqual(errors, expected) self.assertEqual(errors, expected)
def test_pk_not_editable(self):
# PKs cannot be edited in the list.
class SongAdmin(admin.ModelAdmin):
list_display = ["title", "id"]
list_editable = ["id"]
errors = SongAdmin(Song, AdminSite()).check()
expected = [
checks.Error(
"The value of 'list_editable[0]' refers to 'id', which is not editable "
"through the admin.",
obj=SongAdmin,
id="admin.E125",
)
]
self.assertEqual(errors, expected)
def test_editable(self): def test_editable(self):
class SongAdmin(admin.ModelAdmin): class SongAdmin(admin.ModelAdmin):
list_display = ["pk", "title"] list_display = ["pk", "title"]