2013-10-04 16 views
0

我有以下型號:Django的:轉換.Count之間的值返回爲int

class Question(models.Model): 
    question = models.CharField(max_length=100) 

class Answer(models.Model): 
    question = models.ForeignKey(Question) 
    value = models.CharField(max_length=200) 

我要計算與價值「是」爲給定的問題對象答案的%,但我得到的錯誤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, 
    }) 

回答

3

您需要調用count

qc = q.answer_set.count() 

添加()調用count方法。沒有它使得qc參考count方法本身。

下面是一個例子:

>>> a = [1, 2, 3, 4] 
>>> a.clear 
<built-in method clear of list object at 0x02172198> 
>>> a 
[1, 2, 3, 4] 
>>> a.clear() 
>>> a 
[] 
>>> 

正如你可以看到,列表的clear方法僅一次()添加調用。