I use the following get_readonly_fields method to not allow editing of objects in django’s admin interface:
def get_readonly_fields(self, request, obj=None):
if obj == None or request.user.is_superuser:
return self.readonly_fields
# marks all fields as readonly otherwise
fields = [f.name for f in self.model._meta.fields]
return fields
This works perfectly, but the save and save and continue editing still show up. They won’t do anything since all fields are read-only.
Hence my question: Is there a way to hide these save buttons dependent on whether all fields are read-only or not? How could I implement this?
EDIT1:
I’m aware on how to override the admin/submit_line.html template, but what I would like to do instead, is to set the show_save, show_save_as_new to False if I only have read-only fields. How can I change these variable values?
In django/contrib/admin there is a file called submit_line.html which renders the buttons. To override them, in your templates directory, create a folder called admin, and in admin/submit_line.html you would modify it the way you want (based on certain rules). Please note that modifying it this way would affect every admin object-view page.