我找到了,如果有兴趣请评论。
(在 Django 1.7.4 中)扩展表单,代码如下:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for key, field in self.fields.iteritems():
self.fields[key].required = False
class Meta:
model = MyModel
exclude = []
field_1 = forms.CharField(label="field_1_label")
field_2 = forms.CharField(label="field_2_label", widget=forms.Textarea(attrs={'class': 'width100 h4em'}),)
field_3 = forms.CharField(label="field_3_label", widget=forms.TextInput(attrs={'class': 'width100'}),)
field_4 = forms.ModelChoiceField(label='field_4_label', queryset=AnotherModel.objects.all().order_by("order") )
class MyForm_Extended_1(MyForm):
field_1 = None
class MyForm_Extended_2(MyForm):
class Meta:
model = MyModel
exclude =[
'field_1',
]
MyForm_Extended_1 将 field_1 设置为 None,(db 中的列更新为 Null)
MyForm_Extended_2 忽略字段(保存期间忽略db中的列)
因此,出于我的目的,我使用第二种方法。