我有一个表有一个名为first_year_estimate_total和second_year_estimate_total的字段 这需要计算。计算完成所有项目first_year_estimate和second_year_estimate基于类别(land_capital_items_type)
class LandCapital(models.Model):
land_capital_items_type = models.ForeignKey(
LandCapitalItem, blank=True, null=True, related_name='land_capital')
land_item = models.CharField(max_length=200, blank=True, null=True)
first_year_estimate = models.DecimalField(
decimal_places=2, max_digits=20, default=100.00)
second_year_estimate = models.DecimalField(
decimal_places=2, max_digits=20, default=100.00)
first_year_estimate_total = models.DecimalField(
decimal_places=2, max_digits=20, default=100.00)
second_year_estimate_total = models.DecimalField(
decimal_places=2, max_digits=20, default=100.00)
这里first_year_estimate和second_year_estimate必须由用户填写但是 first_year_estimate_total和second_year_estimate_total应基于计算。
例如,如果我有以下数据
land_capital_items_type with item1
CapitalItem1(land_capital_items_type)
item1 100 200
item2 500 700
CapitalItem2(land_capital_items_type)
item1 400 200
item2 600 300
现在CapitalItem1的first_year_estimate_total应该是600(item1的100和item2的first_year_estimate的500)和CapitalItem1的second_year_estimate_total应该是900(item1的200和item2的第2_year_estimate的700)
与CapitalItem2类似
我尝试了以下
LandCapital.objects.aggregate(Sum('first_year_estimate'))
但这会根据能够进入查询集的类别来确定所有项目first_year_estimate的总和
我怎么能这样做?