參考:html
http://www.cnblogs.com/BeginMan/archive/2013/09/10/3312522.htmldjango
(1)、字段類型的對應,django model的字段類型對應到form的字段類型,這裏列舉一下幾個重要的:ui
Model field | Form field |
BooleanField | BooleanField |
CharField | CharField with max_length |
DateField | DateField |
DateTimeField | DateTimeField |
FileField | FileField |
ForeignKey | ModelChoiceField |
IntegerField | IntegerField |
ManyToManyField | ModelMultipleChoiceField |
TextField | CharField with widget=forms.Textarea |
- ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
- ManyToManyField is represented by django.forms.ModelMultipleChoiceField, which is a MultipleChoiceField whose choices are a model QuerySet.
(2)、對應的屬性spa
若是model 字段 有blank=True, 則表單類中對應的要 required=False,不然required=Trueorm
表單字段的 label 被設置爲model field的 verbose_name 且第一個字符大寫.htm
表單字段的 help_text 對應models field的 help_textblog
若是model 字段有choices 則表單字段widget 設置 Selectip