我有以下型号:
class Question(models.Model):
question = models.CharField(max_length=100)
class Answer(models.Model):
question = models.ForeignKey(Question)
value = models.CharField(max_length=200)
我想计算给定问题对象的值为“yes”的答案百分比,但出现错误TypeError int() argument must be a string or a number, not 'method'
。count 不返回 int 数据类型吗?
我的观点:
def questn(request, question_id):
q = Question.objects.select_related().get(id=question_id)
qc = q.answer_set.count
qyc = q.answer_set.filter(value="YES").count
qycp = ( int(qy) / int(qc) ) * 100
return render(request, 'base.html', {
'q':q,
'qc':qc,
'qyc':qyc,
'qycp':qycp,
})