1. 程式人生 > >Django基礎之ModelForm

Django基礎之ModelForm

1. form與model的終極結合

class BookForm(forms.ModelForm):
    class Meta:
        model = models.Book
        fields = "__all__"
        labels = {
            "title": "書名",
            "price": "價格"
        }
        widgets = {
            "password": forms.widgets.PasswordInput(attrs={
                "class": "c1"
            })
        }

2.class Meta下常用引數

model = models.Student  # 對應的Model中的類
fields = "__all__"  #欄位,如果是__all__, 就是表示列出所有的欄位
exclude = None  # 排除的欄位
labels = None   # 提示資訊
help_texts = None   # 幫助提示資訊
widgets = None  # 自定義外掛
error_messages = None   # 自定義錯誤資訊