Django:如何添加自定义按钮来管理更改执行管理操作的表单页面?

dsa*_*laj 16 django django-admin django-admin-actions

我已经为我的模型定义了一个自定义管理操作,它可以完美地按预期工作.我还在SO上看了多个向管理员更改表单页面添加按钮的方法.只有我缺少的一步是如何在更改表单页面中创建一个按钮,使用当前对象执行我的自定义管理操作.

目标是允许管理员单独检查每个对象并对其执行操作,而无需返回列表视图,选择检查对象,以及从列表执行操作.

我的自定义管理操作如下所示:

def admin_apply_change(modeladmin, request, queryset):
    # loop over objects in query set and perform action
Run Code Online (Sandbox Code Playgroud)

我假设有一种简单而干净的方式在管理员更改表单中调用此操作,其中queryset只包含管理员正在查看的当前打开的对象.

注意:如果按钮位于更改表单的底部,Save按钮旁边而不是位于顶部的按钮不是History很明显,那么这将是首选.

请参阅Remi的解决方案.为了使其工作,需要进行以下更正:

1:在重写response_change某些变量的初始化时缺少:

opts = self.model._meta
pk_value = obj._get_pk_val()
preserved_filters = self.get_preserved_filters(request)
Run Code Online (Sandbox Code Playgroud)

2:新的包含标签custom_submit_row应该放在模板标签而不是管理员中(参见自定义模板标签的文档)

3:这是你可以放松一段时间的疏忽.在change_form.html您不仅需要更改建议的行:

{% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %}
Run Code Online (Sandbox Code Playgroud)

而且在底部submit_row出现的更重要的一行:

{% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)

(它位于javascript块的正上方change_form.html)

Rem*_*rra 17

您可以查看change_form_template并将其设置为您的自定义模板并覆盖该response_change方法:

class MyModelAdmin(admin.ModelAdmin):

    # A template for a customized change view:
    change_form_template = 'path/to/your/custom_change_form.html'

    def response_change(self, request, obj):
        opts = self.model._meta
        pk_value = obj._get_pk_val()
        preserved_filters = self.get_preserved_filters(request)

        if "_customaction" in request.POST:
            # handle the action on your obj
            redirect_url = reverse('admin:%s_%s_change' %
                               (opts.app_label, opts.model_name),
                               args=(pk_value,),
                               current_app=self.admin_site.name)
             redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url)
             return HttpResponseRedirect(redirect_url)
        else:
             return super(MyModelAdmin, self).response_change(request, obj)
Run Code Online (Sandbox Code Playgroud)

复制change_form.html你的site-packages/django/contrib/admin/templates/change_form.html并编辑第44行

 {% if save_on_top %}{% block submit_buttons_top %}{% submit_row %}{% endblock %}{% endif %}
Run Code Online (Sandbox Code Playgroud)

 {% if save_on_top %}{% block submit_buttons_top %}{% custom_submit_row %}{% endblock %}{% endif %}
Run Code Online (Sandbox Code Playgroud)

还要检查一下:

 {% block submit_buttons_bottom %}{% submit_row %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)

就在javascript块之上.

然后,您可以在admin.py中的某个位置注册新的包含标记,或将其添加到templatetags:

@register.inclusion_tag('path/to/your/custom_submit_line.html', takes_context=True)
def custom_submit_row(context):
    """
    Displays the row of buttons for delete and save.
    """
    opts = context['opts']
    change = context['change']
    is_popup = context['is_popup']
    save_as = context['save_as']
    ctx = {
        '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': not is_popup and 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:
        ctx['original'] = context['original']
    return ctx
Run Code Online (Sandbox Code Playgroud)

你的内容custom_submit_line.html:

{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_delete_link %}
    {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
    <p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% 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 %}

<input type="submit" value="{% trans 'Custom Action' %}"  name="_customaction" />

</div>
Run Code Online (Sandbox Code Playgroud)

它有很多代码,但主要是复制/粘贴.希望有所帮助.


And*_*ird 7

大多数人可能不假思索地这样做,尽管从答案中不清楚管理员变更表格应该简单地扩展而不是完全覆盖.

custom_change_form.html

{% extends "admin/change_form.html" %}

{% if save_on_top %}{% block submit_buttons_top %}{% custom_submit_row %}{% endblock %}{% endif %}

{% block submit_buttons_bottom %}{% custom_submit_row %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)