django model form 关联

参考:

http://www.cnblogs.com/BeginMan/archive/2013/09/10/3312522.html

(1)、字段类型的对应,django model的字段类型对应到form的字段类型,这里列举一下几个重要的:

 

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)、对应的属性

如果model 字段 有blank=True, 则表单类中对应的要 required=False,否则required=True

表单字段的 label 被设置为model field的 verbose_name  且第一个字符大写.

表单字段的 help_text 对应models field的 help_text

如果model 字段有choices 则表单字段widget 设置 Select

 

 

posted @ 2014-11-09 23:25  Tonyan  阅读(596)  评论(0编辑  收藏  举报