0

我想在父表单中定义的子类中排除特定字段(data_type使用 Select 小部件)。我定义了这样的类:

class ParentForm(forms.ModelForm):
    data_type = forms.CharField(widget=forms.Select(choices=ANNOTATION_TYPES))

    class Meta:
        model = Annotation
        fields = ('data_value','data_type','active','primary_source')                 
        exclude = ()


class DetailForm(ParentForm):

    class Meta(ParentForm.Meta):        
        exclude = ('data_type','primary_source')

这看起来不错:

print DetailForm.Meta.exclude
('data_type', 'primary_source')

但是,当我打印 HTML 时,我仍然会data_typeDetailForm(并且我没有看到其他排除的字段primary_source)中看到该字段:

print DetailForm(None)

HTML:

<tr><th><label for="id_data_value">Data value:</label></th>..
<tr><th><label for="id_active">Active:</label></th>....
<tr><th><label for="id_data_type">Data type:</label></th><td>
<select name="data_type" id="id_data_type">
<option value="Comment1">Subcellular location</option>
<option value="Comment2">Function</option>
<option value="Comment3">Sequence caution</option>
</select></td></tr>

data_type看起来定义的方式有问题。有任何想法吗?

4

1 回答 1

1

由于data_type不是模型字段,因此不起作用。您需要删除以下字段__init__

class DetailForm(ParentForm):
    def __init__(self, *args, **kwargs):
        super(DetailForm, self).__init__(*args, **kwargs)
        del self.fields['data_type']
于 2013-11-07T22:04:15.473 回答