我正在对我的新 Django 代码进行测试。 我有以下型号:
class Places(models.Model):
name = models.CharField(max_length=20)
street = models.CharField(max_length=50)
number = models.IntegerField()
confirmed = models.BooleanField()
notes = models.TextField()
def __unicode__(self):
return self.address
class Meta:
db_table = "Places" # to prevent the prefixes from being added on
当我在 manage.py shell 中执行此操作时,我得到了正确的响应:
from my_app.models import Places
Places.objects.get(name='home').confirmed
>>> True
当在单元测试(manage.py 测试 myapp 下在我的应用程序中执行相同的代码时,我收到错误:
Places matching query does not exist django
当我尝试使用 PDB 时,我发现 Django 将数据库读取为空(一个空集)而不是存储任何数据。
我也在代码中尝试了 RAW SQL,但也失败了,但在 shell 中工作。
我在sql本身中检查了数据库,数据存在。 我执行了等效的SQL代码,它也返回True。
这是 Django 中的一个错误吗?
单元测试始终针对新的空数据库执行。您是否在测试的设置步骤中创建数据,或提供了数据夹具?