Django - clean_fieldname中的条件生成ValidationError时它不应该生成

时间:2015-01-06 17:06:27

标签: django django-forms

我有一个非常基本的clean_fieldname函数:

def clean_fieldname(self):
        data = self.cleaned_data['fieldname']
        if data is not None:
            if data < 0.5 or data > 1:
                raise forms.ValidationError("Please input value between 0.5 and 1")
        return data

fieldname定义如下:

fieldname   = models.DecimalField(max_digits=5,decimal_places=2, default=1)

如果该字段为0.5,则会引发form.ValidationError。怎么会发生这种情况?

我在Python中尝试过,当然0.5不是&lt; 0.5。所以它不应该通过这个条件,但是在Django 1.6中它确实如此。那是为什么?

更新 clean方法被覆盖类似于:

def clean(self):
    cleaned_data = super(MyForm, self).clean()
    fieldname    = cleaned_data.get("fieldname")
    telephone    = cleaned_data.get("telephone")
    myname       = cleaned_data.get("myname")

    if (telephone or myname is not None) and (fieldname is not None):
        raise forms.ValidationError("You forgot to write fieldname.")

    return cleaned_data

0 个答案:

没有答案