Django:在表单中覆盖clean()方法 - 提出错误的问题 [英] Django: Overriding the clean() method in forms - question about raising errors

查看:117
本文介绍了Django:在表单中覆盖clean()方法 - 提出错误的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if self.cleaned_data ['type']。 organ_by!= self.cleaned_data ['organized_by']:
raise forms.ValidationError('类型和组织不匹配')
如果self.cleaned_data ['start']> self.cleaned_data ['end']:
raise forms.ValidationError('开始日期不能晚于结束日期')​​

但是这意味着表单一次只能引发其中一个错误。有没有办法提出这两个错误?



编辑#1
上述任何解决方案很棒,但是会喜欢在一个场景中也可以使用的东西:

  if self.cleaned_data ['type']。organized_by != self.cleaned_data ['organized_by']:
raise forms.ValidationError('类型和组织不匹配')
如果self.cleaned_data ['start']> self.cleaned_data ['end']:
raise forms.ValidationError('开始日期不能晚于结束日期')​​
super(FooAddForm,self).clean()
其中FooAddForm是一个ModelForm,具有唯一的约束,也可能会导致错误。如果有人知道这样的东西,那将是很棒的...

解决方案

从文档:



https://docs.djangoproject.com/en/1.7/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

  from django.forms.util import ErrorList 

def clean(self):

如果self.cleaned_data ['type']。organized_by!= self.cleaned_data ['organized_by']:
msg ='类型和组织不匹配'
self。 _errors ['type'] = ErrorList([msg])
del self.cleaned_data ['type']

如果self.cleaned_data ['start']> self.cleaned_data ['end']:
msg ='开始日期不能晚于结束日期'
self._errors ['start'] = ErrorList([msg])
del self.cleaned_data ['start']

return self.cleaned_data


I've been doing things like this in the clean method:

if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']:
      raise forms.ValidationError('The type and organization do not match.')
if self.cleaned_data['start'] > self.cleaned_data['end']:
      raise forms.ValidationError('The start date cannot be later than the end date.')

But then that means that the form can only raise one of these errors at a time. Is there a way for the form to raise both of these errors?

EDIT #1: Any solutions for the above are great, but would love something that would also work in a scenario like:

if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']:
      raise forms.ValidationError('The type and organization do not match.')
if self.cleaned_data['start'] > self.cleaned_data['end']:
      raise forms.ValidationError('The start date cannot be later than the end date.')
super(FooAddForm, self).clean()

Where FooAddForm is a ModelForm and has unique constraints that might also cause errors. If anyone knows of something like that, that would be great...

解决方案

From the docs:

https://docs.djangoproject.com/en/1.7/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

from django.forms.util import ErrorList

def clean(self):

  if self.cleaned_data['type'].organized_by != self.cleaned_data['organized_by']:
    msg = 'The type and organization do not match.'
    self._errors['type'] = ErrorList([msg])
    del self.cleaned_data['type']

  if self.cleaned_data['start'] > self.cleaned_data['end']:
    msg = 'The start date cannot be later than the end date.'
    self._errors['start'] = ErrorList([msg])
    del self.cleaned_data['start']

  return self.cleaned_data

这篇关于Django:在表单中覆盖clean()方法 - 提出错误的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆