Django测试失败
我在运行Django单元测试时遇到了一个错误,这种情况我以前没有遇到过,今天下午一直在网上查找解决办法。
在终端运行django manage.py test后,我收到了这个错误:
Error: Database test_unconvention couldn't be flushed. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the expected database tables doesn't exist.
* The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.
The full error: (1146, "Table 'test_unconvention.media_image' doesn't exist")
在运行django-admin.py sqlflush时,media_images表被引用,并且在我运行django manage.py syncdb时没有问题。
这是看起来有问题的Image模型:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Image(models.Model):
local_image = models.ImageField(upload_to="uploads/%Y/%m/%d/", height_field="height", width_field="width", max_length=255, null=True, blank=True)
remote_image = models.CharField(editable=False, max_length=255, null=True, blank=True)
thirdparty_page = models.CharField(editable=False, max_length=255, blank=True, null=True)
size = models.CharField(editable=False, max_length=25, blank=True, null=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
height = models.PositiveIntegerField(editable=False, blank=True, null=True)
width = models.PositiveIntegerField(editable=False, blank=True, null=True)
created_at = models.DateTimeField(editable=False, auto_now_add=True)
updated_at = models.DateTimeField(editable=False, auto_now=True)
def __unicode__(self):
if self.local_image:
return self.local_image.name
else:
return self.remote_image
非常感谢任何帮助,如果需要我提供更多信息,请告诉我!
2 个回答
0
试试运行 python manage.py syncdb
,然后再返回去。
2
解决方案:确保你在 INSTALLED_APPS
中明确列出子模块(比如 common.media
),而不仅仅是父模块(比如 common
)。这样可以确保模型被正确识别,测试才能顺利运行。