<p>@Exprator和@Daniel谢谢你的评论。我找到了解决办法:</p>
<p>首先在我的<strong>型号.py</strong>我在课堂上增加了以下内容:</p>
<pre><code>def total_ecof(self):
return self
</code></pre>
<p>然后我改变了我的<strong>视图.py</strong>:</p>
<pre><code>def home(request):
if not request.user.is_authenticated():
template = 'data/login.html'
return render(request, template)
else:
plants = Plant.objects.filter(user=request.user)
for plant in plants:
plant.total_ecof = 0
for recording in Recording.objects.filter(plant__id=plant.id):
plant.total_ecof += (recording.time_to_detect * recording.performance_loss) / (plant.nominal_power * recording.years_of_operation)
template = 'data/home.html'
context = {
'plants':plants
}
return render(request, template, context)
</code></pre>
<p>以及我的html:</p>
<pre><code>{% for plant in plants %}
<tr>
<td>{{ plant.management_company }}</td>
<td>{{ plant.plant_name }}</td>
<td>{{ plant.total_ecof }}</td>
</tr>
{% endfor %}
</code></pre>
<p>我不知道这是否是最好的解决方案,但它是工作:)谢谢你们!!!!!你知道吗</p>