【发布时间】:2020-01-07 07:54:37
【问题描述】:
我创建了几个模型,例如组织。我在测试中注意到的问题是,即使short_description 为空,我也可以在模型上使用create 方法,它会保存一个新实例。
我确实设置了验证器它没有帮助
class Organization(models.Model):
# auto creates with signals pre_save
code = models.CharField(
primary_key=True,
max_length=255,
blank=False,
null=False,
help_text='Unique code or id that can be used to identify organization'
)
name = models.CharField(
max_length=255,
blank=False,
null=False,
help_text='Short name of the organization'
)
short_description = models.CharField(
max_length=255,
blank=False,
null=False,
help_text='Brief overview of the organization',
validators=[MinLengthValidator(1)]
)
测试
# this will pass
@pytest.mark.django_db
def test_organization_create():
obj = Organization.objects.create(name='TEST')
assert obj.code
期望的行为是我创建的 Organization 实例,如果我不指定 short_description 将引发错误。
【问题讨论】:
-
你的意思是“short_description”?
标签: django