【发布时间】:2021-01-12 14:30:47
【问题描述】:
我正在测试一个名为 Category 的 model,它看起来像这样:
class Category(models.Model):
image = models.ImageField(upload_to='images', null=True, blank=True)
title = models.CharField(max_length=200, unique=True, blank=False, null=False)
您可以看到image 字段是可为空的,但是当我尝试在我的TestCase 中创建Category 实例时,我收到以下错误:
raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'image' attribute has no file associated with it.
这是测试:
def setUp(self):
self.category = Category.objects.create(title='test_category')
我已经尝试过这样创建它:
Category.objects.create(title='test_category', image=Null)
但是没有用。
【问题讨论】:
标签: python django django-models django-testing