django-admin: Add extra row with totals

后端 未结 7 1860
盖世英雄少女心
盖世英雄少女心 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条回答
  •  Happy的楠姐
    2020-12-12 21:24

    This thread is going on for a while, but I'm probably not the last one that is looking for such a feature. Therefore, I created a package that makes it easy to add totals.

    Checkout https://github.com/douwevandermeij/admin-totals

    Usage:

    from admin_totals.admin import ModelAdminTotals
    from django.contrib import admin
    from django.db.models import Sum, Avg
    
    @admin.register(MyModel)
    class MyModelAdmin(ModelAdminTotals):
        list_display = ['col_a', 'col_b', 'col_c']
        list_totals = [('col_b', Sum), ('col_c', Avg)]
    

    Feel free to fork it and improve it.

提交回复
热议问题