django-admin: Add extra row with totals

后端 未结 7 1852
盖世英雄少女心
盖世英雄少女心 2020-12-12 20:50

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

7条回答
  •  一生所求
    2020-12-12 21:25

    AFAIK there's no way to do this without creating a custom view. The concept is a little flawed anyway, because a user would expect such a row to show the total for only the visible objects, rather than all the objects in the queryset. Thus, any pagination would result in confusion.

    As an aside, you can add extra columns to an admin list view by doing something like the following:

    class ItemAdmin(admin.ModelAdmin):
        model = Item
    
        list_display = ('field1', 'field2', 'extra_field')
    
        def extra_field(self, obj):
            return u'%s' % do_something_with(obj)
    

提交回复
热议问题