django-admin:添加额外的总计行 [英] django-admin: Add extra row with totals

查看:34
本文介绍了django-admin:添加额外的总计行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准的 django 管理模块来显示行列表.其中一列是数字字段.我想显示一个额外的总计"行,其中大部分列为空白,除了数字列,它应该是所有对象的总数.

I'm using the standard django admin module to display a list of rows. One of the columns is a numerical field. I'd like to display an extra 'totals' row that has most of the columns as blank, except for the numerical column, which should be the total for all of the objects.

在管理模块中是否有一种简单的方法可以做到这一点,或者我最好为它制作一个自定义视图?

Is there a simple way to do this within the admin module, or am I better off making a custom view for it?

我正在使用 Django 1.2.

I'm using Django 1.2.

推荐答案

是的,你可以通过多种方式做到这一点,但大多数 django-ist 的做法是:

Yes, you can do it in many ways, but most django-ist way to do is:

首先覆盖默认的django列表视图...并给出一个新的模板文件目录

First override the default django listing view... And give a new template file directory

ModelAdmin.changelist_view(self, request, extra_context=None)

喜欢:

class MyModelAdmin(admin.ModelAdmin):

    # A template for a very customized change view:
    change_list_template = 'admin/myapp/extras/sometemplate_change_form.html'

    def get_total(self):
        #functions to calculate whatever you want...
        total = YourModel.objects.all().aggregate(tot=Sum('total'))['tot']
        return total

    def changelist_view(self, request, extra_context=None):
        my_context = {
            'total': self.get_total(),
        }
        return super(MyModelAdmin, self).changelist_view(request,
            extra_context=my_context)

因此,您将列表视图上下文添加为总计",以保留您的总价值并将其传递给模板.

So, you add your list view context a 'total' that keeps your total value and pass it to the template.

如果 change_list_template 将设置,django 使用该模板,否则,它使用标准 django 模板.

if change_list_template will set, django uses that template, otherwise, it uses standard django template.

如果 def changelist_view(self, request, extra_context=None) 被调用,django 使用该函数创建内容,否则使用默认 django 视图.

If def changelist_view(self, request, extra_context=None) is called, django uses that function to create the content, otherwise it uses default django views.

然后创建一个 admin/myapp/extras/sometemplate_change_form.html 文件并将您的 {{total}} 放在您想要的任何位置.

Then create a admin/myapp/extras/sometemplate_change_form.html file and place your {{total}} to any place you want.

如何覆盖管理模板的指南这里是如何覆盖管理员视图

更新:我添加了一个简单的 aggregate 来计算总数.您可以对其进行编辑以根据需要进行设置.

UPDATE: I add a simple aggregate to calculate total. you can edit it to set it as your needs.

更新 2:ModelAdmin 模板覆盖选项从 ModelAdmin.change_form_template 固定为 ModelAdmin.change_list_template.(谢谢你 c4urself).是的,但是更改默认的 django 管理模板是一个非常糟糕的选择,因为它被许多其他 ModelAdmin 使用,并且如果更新相关模板可能会导致问题.

UPDATE 2: ModelAdmin template override option fixed from ModelAdmin.change_form_template to ModelAdmin.change_list_template. (thank you c4urself). Yes, but changing the default django admin template is a really bad choice, since it is used by many other ModelAdmin's and it might cause problems if related templates are updated.



注意:
使用过滤器时,总数不会改变,请参阅下面的评论.



NB:
The Total doesn't change when using filters, see comment below.

这篇关于django-admin:添加额外的总计行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆