看了源代码和官方文档,但还是不明白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()
伊谢尔伦2017-04-17 11:42:39
fields 是把MyUser 中的字段拿過來,你用什麼就拿什麼比如MyUser 中有realname , phone 兩個字段,在MyForm 中可以不用在定義了,直接寫在fields = ('realname',' phone',)就可以了