python - 对不属于表单的字段执行 Django 验证
问题描述
我想根据我的 Django 模型中的一个字段提出一个 ValidationError,而不是将相应的文件作为 ModelForm 的一部分。我在谷歌上搜索了一下后发现了模型验证器的概念。所以我尝试执行以下操作:
def minimumDuration(value):
if value == 0:
raise ValidationError("Minimum value accepted is 1 second!")
class PlaylistItem(models.Model):
position = models.IntegerField(null=False)
content = models.ForeignKey(Content, null=True, on_delete=models.SET_NULL)
item_duration = models.IntegerField(validators = [minimumDuration], default = 5, null=True, blank=True)
playlist = models.ForeignKey(Playlist, null=True, on_delete=models.CASCADE)
但是,当我在相应字段中引入 0 时,不会出现错误。从 Django 的文档中,我发现保存模型时不会自动应用验证器。它把我重定向到这个页面,但我真的不明白如何应用这些。任何想法?
解决方案
以下是在模型之外具有此类自定义字段的表单示例:
class ExampleForm(forms.ModelForm):
custom_field = forms.BooleanField(
label='Just non model field, replace with the type you need',
required=False
)
class Meta:
model = YourModel
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# optional: further customize field widget
self.fields['custom_field'].widget.attrs.update({
'id': self.instance.pk + '-custom_field',
'class': 'custom-field-class'
})
self.fields['custom_field'].initial = self._get_custom_initial()
def _get_custom_initial(self):
# compute initial value based on self.instance and other logic
return True
def _valid_custom_field(value):
# validate your value here
# return Boolean
def clean(self):
"""
The important method: override clean to hook your validation
"""
super().clean()
custom_field_val = self.cleaned_data.get('custom_field')
if not self._valid_custom_field(custom_field_val):
raise ValidationError(
'Custom Field is not valid')
推荐阅读
- mysql - 单个查询以获取基于条件的事件和每个事件之后出现的单个事件?
- pandas - 使用带有空值(NaN、NaT)的 `bdate_range`
- html - 向顶部方向增加高度
- snort - 我的 IDS snort.conf 有问题吗?无法在其他 IP 中检测到 Scan Nmap 和 DOS hping
- jquery-query-builder - Querybuilder.js 添加 AND 和 OR 语句
- html - 如何从搜索栏中检索亚马逊的关键字/短语建议
- python - 全局变量函数
- java - 如何使用单个比较的多个 json 路径过滤comapre 2 json?
- javascript - 如何在不使用 JQuery、JSON 或任何外部库的情况下在 JavaScript 中将复选框存储和检索到会话存储?
- excel - 在数据之间插入一行后,我必须在插入的行上方创建数据的数据透视表。当它每周都在变化时,我该怎么做?