Django Admin-禁止编辑并删除“保存”特定型号的按钮 [英] Django Admin- disable Editing and remove "Save" buttons for a specific model
问题描述
我已经在ModelAdmin中单独标记了所有字段,并将has_add_permission重写为:
class SomeModelAdmin(admin.ModelAdmin):
def has_add_permission(self,request):
return False
有没有类似的 has_edit_permission
?哪些可以禁用删除保存和保存并继续按钮?并用简单的关闭和返回按钮替换。
Django文档只提及只读字段而不是覆盖编辑权限。
覆盖 templates / admin / submit_line.html
模板,并按任意所需的按钮。您可以通过将其放在 templates / admin / [app_label] / [model] /submit_line.html
中。
要有条件地显示默认提交行或您的自定义提交行,请覆盖 ModelAdmin.change_view
,并将一个布尔值添加到 extra_context
:
class MyModelAdmin(admin.ModelAdmin):
...
def change_view(self,request,object_id,extra_context = None):
如果不是request.user.is_superuser:
extra_context = extra_context或{}
extra_context ['readonly'] = True
返回超级(MyModelAdmin,self).change_view(request,object_id,extra_context = extra_context)
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.
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 in templates/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 to extra_context
:
class MyModelAdmin(admin.ModelAdmin):
...
def change_view(self, request, object_id, extra_context=None):
if not request.user.is_superuser:
extra_context = extra_context or {}
extra_context['readonly'] = True
return super(MyModelAdmin, self).change_view(request, object_id, extra_context=extra_context)
这篇关于Django Admin-禁止编辑并删除“保存”特定型号的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!