Представления Django Передача переменной через объект
У меня следующая ситуация:
def detail(request, pk):
"""User can view an active survey"""
try:
survey = Survey.objects.prefetch_related("question_set__option_set").get(
pk=pk, creator=request.user, is_active=True
)
except Survey.DoesNotExist:
raise Http404()
questions = survey.question_set.all()
for question in questions:
if question.type != 'Text':
option_pks = question.option_set.values_list("pk", flat=True)
total_answers = Answer.objects.filter(
option_id__in=option_pks).count()
for option in question.option_set.all():
num_answers = Answer.objects.filter(option=option).count()
option.pct = 100.0 * num_answers / total_answers if total_answers else 0
else:
total_answers = Answer.objects.filter(
question_id=question.pk).count()
answers = Answer.objects.filter(
question_id=question.pk).values_list("text", flat=True)
for answer in question.answer_set.all():
num_answers = Answer.objects.filter(
question_id=question.pk, text=answer.text).count()
answer.pct = 100.0 * num_answers / total_answers if total_answers else 0
В случае, если question.type != 'Text', я могу использовать процент в шаблоне... но после "else": , процент не доступен для меня в шаблонах... когда я использую answer_set вопроса вместо option_set
кто-нибудь знает почему??? HELP!!!