Hiding save buttons in admin if all fields are read-only

I use the following get_readonly_fields method to not allow editing objects in the django 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 fine, but save and save and continue editing are still displayed. They will not do anything, since all fields are read-only.

Therefore, my question is: is there a way to hide these save buttons depending on whether all fields are read-only or not? How could I implement this?

EDIT1:

I know how to override the admin/submit_line.html , but instead I would like to set show_save , show_save_as_new to False if I only have read-only fields. How to change these variable values?

+4
source share
1 answer

There is a file in django / contrib / admin called submit_line.html that displays the buttons. To override them, in your templates directory create a folder called admin, and in admin / submit_line.html you change it the way you want (based on certain rules). Note that changing it in this way would affect every page of the admin object view.

+1
source

Source: https://habr.com/ru/post/1432564/


All Articles