如何在多个django模型上应用Sum()的注释?
我有三个django模型:ProjectName、ProjectBudget和ProjectActualCost 项目名称存储项目名称(即项目X)。如何在多个django模型上应用Sum()的注释?,django,Django,我有三个django模型:ProjectName、ProjectBudget和ProjectActualCost 项目名称存储项目名称(即项目X)。 ProjectBudget存储每个预算的项目名称(作为外键(FK))、预算名称以及总预算和日期。ie(项目X,酒店,600),(项目X,租赁,500)。'“600”指600美元。 ProjectActualCost存储发生的各项成本(分项成本)(即酒店:100,租金:50,食品:100)及其日期。因此,它存储“项目名称”(作为FK)、“预算名称”(
ProjectBudget存储每个预算的项目名称(作为外键(FK))、预算名称以及总预算和日期。ie(项目X,酒店,600),(项目X,租赁,500)。'“600”指600美元。 ProjectActualCost存储发生的各项成本(分项成本)(即酒店:100,租金:50,食品:100)及其日期。因此,它存储“项目名称”(作为FK)、“预算名称”(作为FK)、实际使用的项目名称和日期。 ie(项目X,酒店,100,10/03/2019),(项目X,酒店,100,10/06/2019), (项目X,租金,50,04/10/2019) 我试图在html表中呈现“项目名称”、“预算名称”、“总预算”、“总使用量”和“差异”。项目名称、“预算名称”、“总预算”、“总使用量”显示正确,但“差异”显示夸大的金额,如下所示 型号:
class ProjectName(models.Model):
project_name = models.CharField('Name',max_length = 15,blank = False)
def __str__(self):
return self.project_name
class ProjectBudget(models.Model):
project_name = models.ForeignKey(ProjectName,on_delete = models.CASCADE,
null = True)
budget_name = models.CharField('Budget Name'max_length = 50
total_budget = models.DecimalField('Total Budget',max_digits = 9,decimal_places=2)
def __str__(self):
return self.budget_name
class ProjectActualCost(models.Model):
project_name = models.ForeignKey(ProjectName,on_delete = models.CASCADE, null = True)
cost_description = models.ForeignKey(ProjectBudget,on_delete = models.CASCADE,null=True)
actual_used = models.DecimalField('Actual Used',max_digits = 15,decimal_places = 2)
观点:
def budgetview(request,project_id):
budget_items = ProjectBudget.objects.filter(project_name_id=project_id).annotate( actual_cost=Sum('projectactualcost__actual_used'),
difference=Sum('total_budget')-Sum('projectactualcost__actual_used'))
budget_template = "budget_view.html"
context = {"budget_items":budget_items}
return render(request,budget_template,context)
budget_view.html:
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Budget Name</th>
<th>Total Budget</th>
<th>Total Used</th>
<th>Difference</th>
</tr>
<tbody>
{% for item in budget_items%}
<tr>
<td>{{item.project_name}}</td>
<td>{{item.cost_description}}</td>
<td>{{item.total_budget}}</td>
<td>{{item.actual_cost}}</td>
<td>{{item.difference}}</td>
</tr>
{%endfor%}
</tbody>
</table>
但是当我呈现budget_view.html时,我得到:
Project Name|Budget Name | Total Budget| Total Used|Difference|
Project X | Hotel | 600 | 300 | 900
Project X | Rental | 500 | 100 | 600
如何正确计算多个sum()批注?谢谢。这将不起作用,因为您正在聚合ProjectBudget表中的所有行。尝试添加处理每个ProjectBudget实例差异计算的属性
class ProjectBudget(models.Model):
project_name = models.ForeignKey(ProjectName, on_delete=models.CASCADE,
null=True)
budget_name = models.CharField('Budget Name', max_length=50)
total_budget = models.DecimalField('Total Budget', max_digits=9, decimal_places=2)
def __str__(self):
return self.budget_name
@property
def used_difference(self):
return self.total_budget - self.projectactualcost_set.filter(cost_description=self.id)\
.aggregate(Sum('actual_used')).get('actual_used__sum')
并更新模板
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Budget Name</th>
<th>Total Budget</th>
<th>Total Used</th>
<th>Difference</th>
</tr>
<tbody>
{% for item in budget_items %}
<tr>
<td>{{ item.project_name }}</td>
<td>{{ item.budget_name }}</td>
<td>{{ item.total_budget }}</td>
<td>{{ item.actual_cost }}</td>
<td>{{ item.used_difference }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table>
<thead>
<tr>
<th>Project Name</th>
<th>Budget Name</th>
<th>Total Budget</th>
<th>Total Used</th>
<th>Difference</th>
</tr>
<tbody>
{% for item in budget_items %}
<tr>
<td>{{ item.project_name }}</td>
<td>{{ item.budget_name }}</td>
<td>{{ item.total_budget }}</td>
<td>{{ item.actual_cost }}</td>
<td>{{ item.used_difference }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Project Name|Budget Name | Total Budget| Total Used|Difference|
Project X | Hotel | 600 | 250 | 350
Project X | Rental | 500 | 100 | 400