无法隐藏“保存并添加另一个” Django Admin中的按钮 [英] Cannot hide "Save and add another" button in Django Admin
问题描述
我想在满足特定条件时针对特定模型隐藏Django管理员更改表单中的所有保存按钮。因此,我在相关的 ModelAdmin
中重写了 changeform_view
方法,例如:
I would like to hide all the "Save" buttons in Django's Admin's Change Form, for a specific model, when certain conditions are met. Therefore, I override the changeform_view
method in the relevant ModelAdmin
, like so:
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
extra_context = extra_context or {}
obj = collection_management_MammalianLine.objects.get(pk=object_id)
if obj:
if not (request.user.is_superuser or request.user.groups.filter(name='Lab manager').exists() or request.user == obj.created_by):
extra_context['show_save'] = False
extra_context['show_save_and_continue'] = False
extra_context['show_save_and_add_another'] = False
else:
pass
else:
pass
return super(MammalianLinePage, self).changeform_view(request, object_id, extra_context=extra_context)
使用此代码,我可以成功隐藏保存和保存并继续按钮,但是没有t保存并添加另一个。我可以看到 submit_line.html
包含以下三行
With this code, I can successfully hide the "Save" and "Save and continue" buttons, but not the "Save and add another" one. I can see that submit_line.html
contains the following three lines
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
我的问题是:为什么我可以隐藏保存和保存并继续按钮,而不隐藏保存并添加另一个按钮?即使相关的模板标签(show_save_and_continue)在模板中。
My question is: why can I hide the "Save" and "Save and continue" buttons, but not the "Save and add another" one? Even though the relevant templatetag (show_save_and_continue) is in the template.
推荐答案
在传递的上下文中检查其他键,除了 show_save_and_continue
。 Django 始终设置为直接。
The other keys are checked for in the passed context except show_save_and_continue
. Django always sets this directly.
'show_save_and_add_another': (
context['has_add_permission'] and not is_popup and
(not save_as or context['add'])
),
您可以修补 submit_row
模板标记函数首先检查上下文
词典中的 show_save_and_add_another
。 / p>
You can patch the submit_row
template tag function to first check the context
dictionary for show_save_and_add_another
.
@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def submit_row(context):
"""
Display the row of buttons for delete and save.
"""
change = context['change']
is_popup = context['is_popup']
save_as = context['save_as']
show_save = context.get('show_save', True)
show_save_and_continue = context.get('show_save_and_continue', True)
show_save_and_add_another = context.get('show_save_and_add_another', False)
ctx = Context(context)
ctx.update({
'show_delete_link': (
not is_popup and context['has_delete_permission'] and
change and context.get('show_delete', True)
),
'show_save_as_new': not is_popup and change and save_as,
'show_save_and_add_another': (
context.get('show_save_and_add_another', None) or
(context['has_add_permission'] and not is_popup and
(not save_as or context['add']))
),
'show_save_and_continue': not is_popup and context['has_change_permission'] and show_save_and_continue,
'show_save': show_save,
})
return ctx
编辑
修补 admin / submit_line.html包含标签的步骤
-
在
models.py
和views.py <的同一级别上创建
templatetags
文件夹
Create a
templatetags
folder at the same level ofmodels.py
andviews.py
在<$ c中创建 __ init __。py
$ c> templatetags 文件夹
Create __init__.py
in the templatetags
folder
复制 django / contrib / admin / templatetags / admin_modify.py 到 templatetags / admin_modify.py
。
用上面的一个覆盖 submit_row
函数定义。
Overwrite submit_row
function definition with the one above.
请注意,这适用于 Django 2.0 及以下版本。
Note that this is applicable for Django 2.0 and below.
对于最新的Django版本,找到一个上下文组合,使该表达式为 False
。 eg
For recent Django versions, find a context mix that allows this expression to be False
.e.g.
has_add_permission and not is_popup and
(not save_as or add) and can_save
这篇关于无法隐藏“保存并添加另一个” Django Admin中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!