django model form 关联

参考: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

相关文章
相关标签/搜索