小编Mra*_*rak的帖子

Django ModelForm自定义验证:如何访问提交的字段值

我有以下模型表单,并希望将自定义验证添加到名为'billable_work'的字段中.

如何访问表单中提交的字段"项目"?我想检查项目的值(以下示例中的'p'),但无法找到正确的语法,以便我可以测试提交的值.任何帮助,将不胜感激.

class EntryForm(forms.ModelForm):
    class Meta:
        model = Entries
        exclude = ('billable_work','notes')  

    billable_work = forms.BooleanField()
    notes = forms.CharField(widget=forms.Textarea,required=False)

    def clean_billable_work(self):
        b = self.cleaned_data['billable_work']
        p = form.fields['project']

        if b == True and p == 523:
            raise forms.ValidationError(_("Entries cannot be both billable and NONE: Indirect."))
        return self.cleaned_data['billable_work']
Run Code Online (Sandbox Code Playgroud)

django validation modelform

7
推荐指数
1
解决办法
9415
查看次数

标签 统计

django ×1

modelform ×1

validation ×1