我有一个Django模型,我希望它只是readonly.不允许添加和编辑.
我在readAdly中标记了所有字段,并在ModelAdmin中覆盖了has_add_permission:
class SomeModelAdmin(admin.ModelAdmin): def has_add_permission(self, request): return False
有类似的has_edit_permission
吗?可以禁用哪个删除"保存"和"保存并继续"按钮?并通过简单的"关闭并返回"按钮替换.
Django文档仅提及只读字段而不是覆盖编辑权限.
对于Django 1.11:
def has_add_permission(self, request, obj=None): return False def changeform_view(self, request, object_id=None, form_url='', extra_context=None): extra_context = extra_context or {} extra_context['show_save_and_continue'] = False extra_context['show_save'] = False return super(YourModelAdmin, self).changeform_view(request, object_id, extra_context=extra_context)
覆盖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): 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)
我有同样的问题.我在admin.py中修复了它
from django.contrib.admin.templatetags.admin_modify import register, submit_row as original_submit_row @register.inclusion_tag('admin/submit_line.html', takes_context=True) def submit_row(context): ''' submit buttons context change ''' ctx = original_submit_row(context) ctx.update({ 'show_save_and_add_another': context.get('show_save_and_add_another', ctx['show_save_and_add_another']), 'show_save_and_continue': context.get('show_save_and_continue', ctx['show_save_and_continue']), 'show_save': context.get('show_save', ctx['show_save']), 'show_delete_link': context.get('show_delete_link', ctx['show_delete_link']) }) return ctx
在MyModelAdmin类中,添加以下函数
@classmethod def has_add_permission(cls, request): ''' remove add and save and add another button ''' return False def change_view(self, request, object_id, extra_context=None): ''' customize add/edit form ''' extra_context = extra_context or {} extra_context['show_save_and_continue'] = False extra_context['show_save'] = False return super(MyModelAdmin, self).change_view(request, object_id, extra_context=extra_context)
使用Django 1.8(Python 3语法)更新了答案.
有三件事要做:
1)扩展管理员更改表单模板,添加if
有条件地抑制提交按钮
2)覆盖admin.ModelAdmin.change_view()
并设置模板的上下文var if
以读取
3)禁止不需要的POST
请求(来自DOM hacking,curl/Postman )
MyProject的/ my_app应用/模板/管理/程序my_app/change_form.html
{% extends "admin/change_form.html" %} {% load admin_modify %} {% block submit_buttons_top %}{% if my_editable %}{% submit_row %}{% endif %}{% endblock %} {% block submit_buttons_bottom %}{% if my_editable %}{% submit_row %}{% endif %}{% endblock %}
MyProject/my_app/admin.py(MyModelAdmin)
def change_view(self, request, object_id, form_url='', extra_context=None): obj = MyModel.objects.get(pk=object_id) editable = obj.get_status() == 'Active' if not editable and request.method == 'POST': return HttpResponseForbidden("Cannot change an inactive MyModel") more_context = { # set a context var telling our customized template to suppress the Save button group 'my_editable': editable, } more_context.update(extra_context or {}) return super().change_view(request, object_id, form_url, more_context)
我有同样的问题 - 最简单的方法是包含一些自定义JS.
在你admin.py文件包含
class Media: js = ('/static/js/admin.js',)
然后在您的admin.js文件中,包含以下JS.
(function($) { $(document).ready(function($) { $(".submit-row").hide() }); })(django.jQuery);
这行已经消失了 - 它也适用于所有版本的Django.