使用Django admin的自定义列

时间:2012-02-06 17:46:54

标签: python django django-admin

我有一个模型Data,与这样的表相关联(模型Data仅由IntegerField组成):

subject | year | quarter | sales |
----------------------------------
   1    | 2010 |   1     | 20    |
   1    | 2010 |   2     | 100   |
   1    | 2010 |   3     | 100   |
   1    | 2010 |   4     | 20    |
   1    | 2011 |   1     | 30    |
   1    | 2011 |   2     | 50    |
   1    | 2011 |   4     | 40    |
   2    | 2010 |   1     | 30    |
   2    | 2010 |   2     | 20    |
 [..-GO ON this way...]

我想要一个django-admin表,只读列(current year = 2011, quarter = 1

subject | sales current year | sales current quarter | sales last year | sales current quarter last year |
----------------------------------------------------------------------------------------------------------
  1     |  110               |  30                   |  240            |  20
[AND SO ON]

问题是:使用django-admin可以吗?出路是什么?

2 个答案:

答案 0 :(得分:34)

您可以将ModelModelAdmin上的方法用作list_display的项目。请参阅:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

由于这些方法在管理员之外可能很有用,我建议您将它们添加到Model

from django.db.models import Sum

class Data(models.Model):
    ...

    # Method used by `get_current_year_sales` and `get_last_year_sales`
    # to stay DRY. Not for use directly in admin.
    def get_year_sales(self, year):
        qs = self.model._default_manager.filter(year=year)
        sales_agg = qs.aggregate(Sum('sales'))
        return sales_agg['sales__sum']

    # Method used by `get_current_quarter_sales` and `get_last_quarter_sales`
    # to stay DRY. Not for use directly in admin.
    def get_quarter_sales(self, year, quarter):
        qs = self.model._default_manager.filter(year=year, quarter=quarter)
        sales_agg = qs.aggregate(Sum('sales'))
        return sales_agg['sales__sum']

    def get_current_year_sales(self):
        return self.get_year_sales(datetime.now().year)
    get_current_year_sales.short_description = 'Sales (Current Year)'

    def get_last_year_sales(self):
        return self.get_year_sales(datetime.now().year-1)
    get_last_year_sales.short_description = 'Sales (Last Year)'

    def get_current_quarter_sales(self):
        # Determine current quarter logic here as `current_quarter`
        # `quarter_year` will likely be same as current year here,
        # but will need to be calculated for previous quarter
        return self.get_quarter_sales(quarter_year, current_quarter)
    get_current_quarter_sales.short_description = 'Sales (Current Quarter)'

    def get_current_quarter_sales(self):
        # Logic here to determine last quarter as `last_quarter`
        # Logic to determine what year last quarter was in as `quarter_year`
        return self.get_quarter_sales(quarter_year, last_quarter)
    get_last_quarter_sales.short_description = 'Sales (Last Quarter)'

short_description属性确定管理员将显示为这些方法的行标题。因此,一旦完成所有这些操作,您只需修改ModelAdmin的{​​{1}}属性,例如:

list_display

答案 1 :(得分:8)

这样的事情应该有效(未经测试):

# models.py
class Data(models.Model):
    year = models.DateField()
    sales = models.IntegerField()
    # ...

    def sales_current_year(self):
        return self.model._default_manager.get_queryset().filter(year=2012).annotate(Sum('sales'))

 # admin.py
 class DataAdmin(admin.ModelAdmin):
      list_display = ('sales_current_year',)