看了源代码和官方文档,但还是不明白fields到底改变了什么?
'fields' is an optional list of field names. If provided, only the named fields will be included in the returned dict.
class MyForm(forms.ModelForm):
realname = forms.CharField()
phone = forms.CharField()
class Meta:
model = MyUser
fields = ('phone',)
form = MyForm()
# 照样会输出realname
form.as_table()
fields 是把MyUser 中的字段拿过来,你用什么就拿什么 比如MyUser 中有realname , phone 两个字段,在MyForm 中可以不用在定义了,直接写在 fields = ('realname','phone',)就可以了