【问题标题】:Django - customised validation errorDjango - 自定义验证错误
【发布时间】:2014-11-06 04:02:19
【问题描述】:

我正在尝试将自定义验证添加为我的表单的一部分。

当 自愿日期显示类型 是指定数字时,我正在尝试触发自定义验证。但是,当我运行以下代码时, volunction_date_display_type 值是 None 并且我期待一个数字/数字。

我已经阅读了form field validation 上的 django 文档,但我看不到我的错误。

目前只有最后一个else条件被触发,因为值为None。

谁能指出我做错了什么?

这是我的 forms.py 文件中的代码:

class Meta:
    model = VoluntaryDetails

    fields = (
        .......
        'voluntary_date_display_type',
        .......
    )

def clean_voluntary_finish_date(self):

    voluntary_display_type = self.cleaned_data.get('voluntary_display_type')
    voluntary_start_date = self.cleaned_data.get('voluntary_start_date')
    voluntary_finish_date = self.cleaned_data.get('voluntary_finish_date')
    voluntary_date_display_type = self.cleaned_data.get('voluntary_date_display_type')

    if voluntary_display_type == 0:
        if voluntary_finish_date is not None and voluntary_start_date is not None:
            if voluntary_start_date > voluntary_finish_date:
                if voluntary_date_display_type == 2 or voluntary_date_display_type == 3:
                    raise forms.ValidationError(_("To Date must be after the From Date."))
                elif voluntary_date_display_type == 4 or voluntary_date_display_type == 5:
                    raise forms.ValidationError(_("Finish Date must be after the Start Date."))
                elif voluntary_date_display_type == 6 or voluntary_date_display_type == 7:
                    raise forms.ValidationError(_("End Date must be after the Begin Date."))
                elif voluntary_date_display_type == 8:
                    raise forms.ValidationError(_("This Date must be after the other Date."))
                elif voluntary_date_display_type == 9 or voluntary_date_display_type == 10:
                    raise forms.ValidationError(_("This Duration date must be after the other Duration date."))
                else:
                    raise forms.ValidationError(_("Completion Date must be after the Commencement Date."))

    return voluntary_finish_date

【问题讨论】:

    标签: django validation django-forms django-validation


    【解决方案1】:

    clean_voluntary_finish_date 仅在验证特定字段时调用,因此其他字段可能尚未“清理”。这意味着当你使用self.cleaned_data.get('voluntary_date_display_type')时,该字段还没有被清理,所以cleaned_data中没有key,.get()方法会返回None

    验证依赖多个字段时,需要使用普通的clean()方法;如 “清理和验证相互依赖的字段”下的 django 表单 reference 中所述

    假设我们在联系表单中添加另一个要求:如果 cc_myself 字段为 True,主题必须包含“帮助”一词。我们 一次对多个字段执行验证,因此 form 的 clean() 方法是执行此操作的好地方。 请注意,我们是 在这里讨论表单上的 clean() 方法,而之前我们 在字段上编写 clean() 方法。 保持 确定验证位置时,字段和表单差异清晰 事物。字段是单个数据点,表单是一个集合 字段。

    当表单的 clean() 方法被调用时,所有个人 将运行字段清理方法(前两节),所以 self.cleaned_data 将填充任何幸存的数据,因此 远的。所以你还需要记住考虑到 您要验证的字段可能无法在初始状态下存活 单独的现场检查。

    你所要做的就是:

    def clean(self):
        cleaned_data = super(YourFormClassName, self).clean()
        # copy and paste the rest of your code here
        return cleaned_data # this is not required as of django 1.7
    

    【讨论】:

      猜你喜欢
      • 2015-03-17
      • 2011-06-15
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 2012-05-23
      • 1970-01-01
      • 2017-09-10
      相关资源
      最近更新 更多