0
型號:複雜的款項按月劃分
class Category(models.Model):
name = models.CharField(max_length=100)
class Operation(models.Model):
date = models.DateField()
value = models.DecimalField(max_digits = 9, decimal_places = 2)
category = models.ForeignKey(Category, null = True)
comments = models.TextField(null = True)
現在我想創建一個視圖,其中13列:
類的名稱| -11 | -10 | -9 | ... | -1 | 0
例如。
...食品.. | $ 123.00 | $ 100.14 | ... | $ 120.13 | $ 54.12
.clothes。| $ 555.23 | $ 232.23 | ... | $ 200.12 | $ 84.44
其中$ 123.00例如是11個月前製造的類別食品的操作值的總和$ 100.14 - 10個月前等等 - $ 54.12是當前月份的總和555.23 =>類似的衣服。 ..
我GOOGLE了很多,但大多數的例子是簡單的 - 沒有相關類(類別)
回答1的建議後正確答案:
def get_month_sum_series(self):
import qsstats, datetime
from django.db.models import Sum
qss = qsstats.QuerySetStats(self.operation_set.all(), date_field='date', aggregate_field='value',aggregate_class=Sum)
today = datetime.date.today()
year_ago = today - datetime.timedelta(days=365)
return qss.time_series(start_date=year_ago, end_date=today, interval='months')
非常接近,但不工作 - 請看後 - 我添加方法...... – 2012-04-21 17:46:58
YES!我不應該使用operation__value - 它只是價值; D – 2012-04-21 17:54:32