Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过验证继承django表单_Django - Fatal编程技术网

通过验证继承django表单

通过验证继承django表单,django,Django,我有一种形式必须由第二种形式继承。父项的字段以及验证必须在子项中处于活动状态 这是一个表单示例,其中包含我希望继承的字段和验证行为: class INeedForm(forms.Form): i_need = forms.ChoiceField( label = "I need", choices= [ ("select", "Select"), ("only", "The only choice"), ],

我有一种形式必须由第二种形式继承。父项的字段以及验证必须在子项中处于活动状态

这是一个表单示例,其中包含我希望继承的字段和验证行为:

class INeedForm(forms.Form):
    i_need = forms.ChoiceField(
        label = "I need",
        choices= [
        ("select", "Select"),
        ("only", "The only choice"),
        ],
        initial = "select",
        widget = forms.Select(attrs = {'class' : 'form-control'}))

    def clean(self):
        cleaned_data = super(INeedForm, self).clean()

        i_need = cleaned_data.get("i_need")
        if i_need == "select":
            self.add_error('i_need', "Please select one")

在方法
clean
的末尾,您应该返回
cleaned\u数据

您可以继承您的
INeedForm
及其行为:

class NewForm(INeedForm):
    # If you need inherit Meta
    class Meta(INeedForm.Meta):
        pass

好啊那么,当你尝试这种方法时,什么是不起作用的呢?