[英]Add “Save as new” button to Django foreign object edit popup
In Django we have save_as
parameter for ModelAdmin, that enables "Save as new" button on the admin's site edit page of some object. 在Django中,我们为ModelAdmin提供了save_as
参数,该参数在某个对象的管理员站点编辑页面上启用“另存为新”按钮。
But when the object (Model1 instance) is in relations with other model (Model2) by default, and I want to edit Model2 instance in following way: take default for the current relation (depends on Model2 instance fields) Model1 instance and edit some of its fields, I click edit button and the popup appears, where I can change some fields, but can't save that object as new, so I have 2 option: corrupt the default object or copy-paste each field of that related object into "Add new" popup. 但是,当对象(Model1实例)默认情况下与其他模型(Model2)关联时,我想按以下方式编辑Model2实例:将当前关系设为默认值(取决于Model2实例字段)Model1实例并编辑一些在其字段中,单击“编辑”按钮,将出现弹出窗口,可以在其中更改某些字段,但不能将该对象另存为新对象,因此我有2个选项:破坏默认对象或将相关对象的每个字段复制粘贴到其中“添加新”弹出窗口。
I want to add "Save as new" button into Edit popup. 我想在“编辑”弹出窗口中添加“另存为新”按钮。
I have found, that adding hidden input with name="_saveasnew"
and value=1
is actually does save another model, but how to add it among with special button? 我发现,使用name="_saveasnew"
和value=1
添加隐藏的输入实际上可以保存另一个模型,但是如何在特殊按钮之间添加它呢?
Also, there is special block on that button for popup window, as I see: https://github.com/django/django/blob/1.8/django/contrib/admin/templatetags/admin_modify.py#L39 (yes, we use Django 1.8 :() What is the best solution: edit the template or edit the tag itself? 另外,在弹出窗口的按钮上有一个特殊的块,如我所见: https : //github.com/django/django/blob/1.8/django/contrib/admin/templatetags/admin_modify.py#L39 (是的,我们使用Django 1.8 :()最好的解决方案是:编辑模板或编辑标签本身?
The right solution is to use your own submit_row
template tag for popup window, to do it, you have to override admin/change_form.html
template's blocks: submit_buttons_top
and submit_buttons_bottom
: 正确的解决方案是对弹出窗口使用自己的submit_row
模板标签,要做到这一点,您必须覆盖admin/change_form.html
模板的块: submit_buttons_top
和submit_buttons_bottom
:
{% extends "admin/change_form.html" %}
{% load YOUR_TEMPLATE_TAGS %}
{% block submit_buttons_top %}{% YOUR_SUBMIT_ROW_TAG %}{% endblock %}
{% block submit_buttons_bottom %}{% YOUR_SUBMIT_ROW_TAG %}{% endblock %}
Main difference in code of new submit_row
tag is changed context key: 'show_save_as_new': change and save_as,
新的submit_row
标记的代码主要区别在于更改了上下文键: 'show_save_as_new': change and save_as,
Code of YOUR_TEMPLATE_TAGS.py
could look something like this: YOUR_TEMPLATE_TAGS.py
代码可能类似于以下内容:
from django import template
register = template.Library()
@register.inclusion_tag('admin/submit_line.html', takes_context=True)
def YOUR_SUBMIT_ROW_TAG(context):
"""
Displays the row of buttons for delete and save.
HINT: override of default django `submit_row` tag, changes:
`save as new` button is displayed for popup.
"""
opts = context['opts']
change = context['change']
is_popup = context['is_popup']
save_as = context['save_as']
new_context = {
'opts': opts,
'show_delete_link': (
not is_popup and context['has_delete_permission'] and
change and context.get('show_delete', True)
),
'show_save_as_new': change and save_as,
'show_save_and_add_another': (
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'],
'is_popup': is_popup,
'show_save': True,
'preserved_filters': context.get('preserved_filters'),
}
if context.get('original') is not None:
new_context['original'] = context['original']
return new_context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.