2013-10-20 62 views
1

型号:获取或创建 - 不支持的操作数类型(S)为+ =: 'builtin_function_or_method'

class TestVersion(models.Model): 
    test = models.ForeignKey(Test) 
    count = models.IntegerField(default=0) 

观点:

test = Test.objects.get(id=id) 
result = TestVersion.objects.get_or_create(test=test) 
result.count += 1 
result.save() 

我有这样的错误:

unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'

on line:result.count += 1

如何解决它?

回答

2

尝试:result, created = TestVersion.objects.get_or_create(test=test)

get_or_create返回(对象,创建的),其中对象被检索到或创建的对象和创建是一个布尔值指定一个新的对象是否已创建的元组。

看看这里的参考:https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create

+0

'count'是现场,没有方法 – orion95

+0

的名字@ orion95见上面我的更新 –

相关问题