html - Django: ModelForm中Meta的fields成员有什么作用?
大家讲道理
大家讲道理 2017-04-17 11:40:39
0
1
483

看了源代码和官方文档,但还是不明白fields到底改变了什么?

django/forms/models.py:

'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()
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
伊谢尔伦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template