Я пытаюсь получить процент, но я не знаю, как это сделать для моих моделей.Я пытался использовать некоторый код, но я получаю ошибку.Как я могу получить процент за мои модели?
models.py
class Choice(models.Model):
question = models.ForeignKey('Question', models.CASCADE)
message = models.CharField(max_length=1024)
ballots = models.IntegerField(null=True, blank =True)
views.py
def ReportList(request, id):
q = Question.objects.select_related().get(id = id)
queryset = Choice.objects.filter(question_id=id)
if not queryset:
return redirect ('error')
return render(request, 'polls/report.html', {'queryset': queryset, 'q': q})
html
{% for choice in queryset %}
<tr>
<td>{{ choice.message }}</td>
<td>{{ choice.ballots }}</td>
</tr>
{% endfor %}
Я пытался использовать этот код в models.py, но я думаю, что это неправильный путь:
def get_percentage(self):
total_count = Choice.objects.annotate(sum('ballots'))
cnt = Choice.ballots.__float__()
perc = cnt * 100 / total_count
return perc