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.
Updated answer using Django 1.8 (Python 3 syntax).
There are three things to do:
1) extend the admin change form template, adding an
if
to conditionally suppress the submit buttons2) override
admin.ModelAdmin.change_view()
and set a context var for the templateif
to read3) prohibit unwanted
POST
requests (from DOM hacking, curl/Postman)MyProject/my_app/templates/admin/my_app/change_form.html
MyProject/my_app/admin.py (MyModelAdmin)
For Django 1.11:
I had the same problem - the easiest way to do this, is to include some custom JS.
In you admin.py file include
Then in your admin.js file, include the following JS.
The row is gone - it should work in all versions of Django too.
I had same problem. I fixed it in admin.py
In MyModelAdmin class, add following function
Override the
templates/admin/submit_line.html
template and make the buttons whatever you want. You can do this for only the specific model by putting it intemplates/admin/[app_label]/[model]/submit_line.html
.To conditionally show the default submit line or your custom submit line, override
ModelAdmin.change_view
, and add a boolean toextra_context
:You could try this package Django Admin View Permission. This package adds a
view permission
for the specified models and handles the other stuff automatically.