I have a Django Model which I wish to be only readonly. No adds and edits allowed.
I have marked all fields readonly and overridden has_add_permission in ModelAdmin as:
class SomeModelAdmin(admin.ModelAdmin):
def has_add_permission(self, request):
return False
Is there a similar has_edit_permission
? Which can be disabled to remove "Save" and "Save and continue" buttons? And replace by a simple "Close and Return" button.
Django Documentation Only mentions only about read only fields not about overriding edit permissions.
Based on the excellent answer from @mat_gessel, here's my solution:
The main differences are UX'y:
HttpResponseForbidden
to prevent a saveAlso:
read_only
is such a useful, non-invasive enhancementrequest.method != 'GET'
to preventPATCH
and friends (not altogether sure if this is required, tbh)my_app/admin.py
admin/change_form.html
(Tested on Django 1.9: heads up: some imports have moved since then, eg
reverse
)