添加复选框并删除自定义Django admin change_list的操作

Hak*_*kim 10 python django django-admin

我已经定制Django的change_list.html下面这个教程.我的问题涉及该教程中未涉及的内容:

如何轻松添加checkbox和actions(删除所选项目)?

我查看templatetags了管理部分(主要是在这里,但我无法理解如何轻松地将删除操作添加到自定义change_list.html模板中的每个项目以及应该添加到ModelAdmin类中的内容).

更新:

下面是自定义change_list.html,我正在尝试添加项目复选框:

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

{% block content_title %}
    <h1>Title</h1>
{% endblock %}

{% block result_list %}
    <div class="results">
        <table id="result_list">
            <thead>
                ...
            </thead>

            <tbody>
                {% for item in items %}
                    <tr class="{% cycle 'row1' 'row2' %}">
                        ...
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

Sab*_*dov 6

关键是看“sale_summary_change_list.html”中扩展的“admin/change_list.html”模板。它的 result_list 块具有所需的形式。您还必须将输入复选框添加到 admin.py/changelist_view 中返回的查询集中。我修改了教程中的代码。如果我们希望能够删除单个项目,我们当然必须放弃销售聚合。

from django.contrib import admin
from django.contrib.admin import ModelAdmin, helpers

from .models import SaleSummary, Category


@admin.register(SaleSummary)
class SaleSummaryAdmin(ModelAdmin):
    change_list_template = 'admin/sale_summary_change_list.html'
    date_hierarchy = 'date'

    def changelist_view(self, request, extra_context=None):
        response = super(SaleSummaryAdmin, self).changelist_view(
            request,
            extra_context=extra_context,
        )
        try:
            qs = response.context_data['cl'].queryset
        except (AttributeError, KeyError):
            return response

        # metrics = {
        #     'total': Count('id'),
        #     'total_sales': Sum('amount'),
        # }
        result_qs = list(qs.values('category__name', 'pk', 'amount').order_by('category__name').all())
        map(lambda r: r.update(
            {'check_box': helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, r['pk'])}), result_qs)
        response.context_data['summary'] = list(result_qs)

        return response
Run Code Online (Sandbox Code Playgroud)

这是模板:

{% extends "admin/change_list.html" %}
{% load humanize admin_list%}
{% block content_title %}
    <h1> Sales Summary </h1>
{% endblock %}

{% block result_list %}

          {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
          {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
    <div class="results">
        <table>
            <thead>
            <tr>
                <th>
                    <div class="text">
                        <a href="#">Action</a>
                    </div>
                </th>
                <th>
                    <div class="text">
                        <a href="#">Category</a>
                    </div>
                </th>
                <th>
                    <div class="text">
                        <a href="#">Total Sales</a>
                    </div>
                </th>
            </tr>
            </thead>
            <tbody>
            {% for row in summary %}
                <tr class="{% cycle 'row1' 'row2' %}">
                    <td> {{ row.check_box }} </td>
                    <td> {{ row.category__name }} </td>
                    <td> {{ row.amount | intcomma }} </td>

                </tr>
            {% endfor %}
            </tbody>

        </table>
    </div>

{% endblock %}

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

在github上查看完整的项目:

https://github.com/SabirAkhadov/django-action-change-list-demo