看了源代码和官方文档,但还是不明白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 is to take the fields from MyUser. You can use whatever you want. For example, MyUser has two fields: realname and phone. They don’t need to be defined in MyForm. They can be written directly in fields = ('realname',' phone',) is fine