如何在django的modelsclean
方法中引发字段绑定ValidationException
?在
from django.core.exceptions import ValidationError
def clean(self):
if self.title:
raise ValidationError({'title': 'not ok'})
上面的操作不会将错误添加到title
字段(使用表单时),而是添加到非字段错误(__all__
)。在
我知道如何在表单(self._errors['title'] = self.error_class([msg])
)内完成,但是在modelsclean
方法中,self._errors
不存在。在
不需要,
Model
的clean方法只用于引发non field errors
,但是可以通过创建clean_title
方法来引发字段错误。在根据Django文档,使用 model.clean()
这提供了你所要求的一切!在
注释上方的框似乎是您要查找的内容:
code参数是kwarg,因此是可选的。(它在示例中,所以我粘贴在上面)
我猜你需要这样的东西:
^{pr2}$相关问题 更多 >
编程相关推荐