我的model.py是:
Sample(models.Model):
id=models.DecimalField(max_digits=8, decimal_places=0, primary_key=True)
name=models.CharField(max_length=12)
length=models.DecimalField(max_digits=8, decimal_places=0)
def save(self):
super(Sample,self).save()
在我的tests.py中:
class SampleTestCase(TestCase):
def setUp(self):
self.sample=Sample.objects.create(id=12,name='xyz', length=33)
print self.sample.length
print "hello in setup"
我的问题是,当我使用表单或使用管理员时,数据已成功保存而没有任何错误,但是当我尝试对其进行测试时,它会引发错误:
self.sample=Sample.objects.create(id='12',name='iftikhar', length=33)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 149, in create
return self.get_query_set().create(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 414, in create
obj.save(force_insert=True, using=self.db)
TypeError: save() got an unexpected keyword argument 'force_insert'
如果我从models.py中删除保存功能,任何人都可以告诉我为什么会发生这种情况,测试运行成功