<table class="table">
<thead>
<tr>
<th>School</th>
<th>Strength</th>
<th>Average</th>
</tr>
</thead>
<tbody>
{% for school in cluster.school_set.all %}
<tr>
<td><a href="{% url 'data:school_detail' state.id region.id cluster.id school.id %}">{{ school.school_name }}</a></td>
<td>{{ school.strength }}</td>
<td>school average</td>
</tr>
{% endfor %}
</tbody>
</table>
這是在我的模板中的表,它表明在羣集中的所有學校的詳細信息(市/鎮)以表格形式:1)學校名稱列2)學校力量3)學校平均分數從一定數量的考試。 我可以很容易地得到列的數據1)學校名稱& 2)學校的實力,因爲他們只是上課的領域。問題在於列平均值。爲了獲得平均水平,我需要有一些我無法直接在模板中完成的代碼。問題是我不知道如何在views.py中做到這一點。來計算,我需要school.id,但從URL我只得到cluster.id 在這些情況下,你不能簡單地從views.py中獲取數據,而不從模板中給出值怎麼辦?在Django項目顯示從views.py表
set = Average.objects.filter(school=school.id):
obj = set.latest('average_date')
avg = obj.average_value
這些是我需要得到一個特定學校的平均代碼行。
class School(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
school_name = models.CharField(max_length=250)
facilitator = models.CharField(max_length=250)
f_number = models.IntegerField()
f_email = models.EmailField()
school_logo = models.FileField(default='')
strength = models.IntegerField()
def __str__(self):
return self.school_name
class Avergae(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
school = ChainedForeignKey(School, chained_field="cluster",chained_model_field="cluster", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
average_date = models.DateField()
average_value = models.DecimalField(max_digits=4, decimal_places=2)
average_attendance = models.IntegerField()
average_note = models.CharField(max_length=250)
def __str__(self):
return '{}: {}'.format(self.average_date, self.average_value)
這些都是我的模特學校和平均
您可以在'school'模型中創建方法或創建django自定義過濾器,請顯示您的'Average'計算示例和學校模型,請 –
是的,發佈模型和視圖。 – MadPhysicist
我剛更新了問題請看看 –