gpt4 book ai didi

python - Django 模型选择不会为无效选择引发错误

转载 作者:太空狗 更新时间:2023-10-30 00:50:34 25 4
gpt4 key购买 nike

我在 Django 中有一个带有选择字段的对象

class CustomFieldType(models.Model):
STRING = 'STRING'
DATE = 'DATE'
BOOLEAN = 'BOOLEAN'
NUMERIC = 'NUMERIC'
EMAIL = 'EMAIL'
TYPE_CHOICES = (
(STRING, _('String')),
(DATE, _('Date')),
(BOOLEAN, _('Boolean')),
(NUMERIC, _('Numeric')),
(EMAIL, _('Email'))
)
name = models.CharField(max_length=256)
field_type = models.CharField(choices=TYPE_CHOICES, default=STRING, max_length=10)
company = models.ForeignKey('Company')

class Meta:
unique_together = ('name', 'company')

def __unicode__(self):
return self.name

在我的 django 控制台中

$> CustomFieldType.objects.create(name='custom_name',field_type='noError',company=mycompany)
<CustomFieldType: custom_name>
$> CustomFieldType.objects.get(name='custom_name').field_type
u'noError'

为什么 django 没有报错(ValidationError)?还是我错过了什么?

最佳答案

choices选项仅用于预填充表单下拉字段;它不强制执行任何验证:

If this is given, the default form widget will be a select box with these choices instead of the standard text field.

关于python - Django 模型选择不会为无效选择引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com