Opened 7 hours ago
Closed 3 hours ago
#37011 closed Bug (needsinfo)
The presence of a non-readonly non-editable field in ModelAdmin fails at runtime and has no system check
| Reported by: | Andrew Northall | Owned by: | |
|---|---|---|---|
| Component: | contrib.admin | Version: | 6.0 |
| Severity: | Normal | Keywords: | system check |
| Cc: | Andrew Northall | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
In ModelAdmin, several field configuration errors (such as adding an non-existent field to a ModelAdmin fields or fieldsets attribute) are covered by system checks in contrib/admin/checks.py. We also have a system check for non-editable fields in the list_editable attribute (E125). However, a common runtime failure is that a model field which has editable=False set in the model is either:
- added to a
ModelAdminwhich does not specifyfieldsorfieldsetsattributes and does not include it inreadonly_fieldsorexcludes, or - added to a
ModelAdminwhich specifiesfieldsorfieldsetsattributes with this non-editable field included, but the field is omitted fromreadonly_fields.
In simpler terms, any time a field which is non-editable on the model, and not marked as read only appears in change form, this will result in the following error when accessing the create or update views for this ModelAdmin:
FieldError at /admin/app/modelname/add/ 'fieldname' cannot be specified for ModelName model form as it is a non-editable field. Check fields/fieldsets/exclude attributes of class ImageTestAdmin.
It would make sense to me that, at a minimum, we should add a system check for this error to bring the detection in line with other ModelAdmin field configuration errors and avoid silent failures at runtime - or perhaps there could be a discussion as to whether the default behaviour could or should be to simply mark these fields as read only by default?
Change History (1)
comment:1 by , 3 hours ago
| Keywords: | system check added |
|---|---|
| Resolution: | → needsinfo |
| Status: | new → closed |
Hello Andrew, thank you for the ticket. After an initial read, the request makes sense but to fully accept this ticket, we'd need either a failing test case or a small and minimal Django project to reproduce what you describe. I'll close as
needsinfofor now, but please reopen when you are available to provide the reproducer.