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

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

问题描述

我使用标准的django管理模块来显示行列表。其中一列是一个数字字段。我想显示一个额外的总计行,其中大多数列为空,除了数字列,这应该是所有对象的总和。



我正在使用Django 1.2。

解决方案

是的,你可以在很多方面做,但大多数django-ist的方法是:



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

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

喜欢:

  class MyModelAdmin(admin.ModelAdmin):

#非常自定义更改视图的模板:
change_list_template = 'admin / myapp / extras / sometemplate_change_form.html'

def get_total(self):
#函数来计算你想要的东西...
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(),
}
返回超级(MyModelAdmin,self).changelist_view(request,
extra_context = my_context)

所以,你添加列表视图上下文,总数值并传递给模板。



如果 change_list_template 将设置,django将使用该模板,否则使用标准django模板。 / p>

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



然后创建一个 admin / myapp / extras / sometemplate_change_form.html 文件,并将您的 {{总}} 到任何您想要的地方。



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



更新:我添加一个简单的 aggregate 来计算总计。您可以编辑它以将其设置为您的需要。



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




NB :_
使用过滤器时,总计不会改变,见下面的评论。


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?

I'm using Django 1.2.

解决方案

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

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

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

Like:

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.

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

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.

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

A guide to how to override admin templates And here is how to override admin views

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

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天全站免登陆