Table B.2 shows the mapping of form field objects to model field objects. This mapping is used when you use the form_for_model and form_for_instance helper functions. This table might also help you know which Field objects to use when you create custom forms.
Model Field | Form Field |
---|---|
AutoField | Not represented in the form |
BooleanField | BooleanField |
CharField | CharField with max_length set to the model field's max_length |
CommaSeparatedIntegerField | CharField |
DateField | DateField |
DateTimeField | DateTimeField |
DecimalField | DecimalField |
EmailField | EmailField |
FileField | FileField |
FilePathField | CharField |
FloatField | FloatField |
ForeignKey | ModelChoiceField |
ImageField | ImageField |
IntegerField | IntegerField |
IPAddressField | IPAddressField |
ManyToManyField | ModelMultipleChoiceField |
NullBooleanField | CharField |
PhoneNumberField | USPhoneNumberField |
PositiveIntegerField | IntegerField |
PositiveSmallIntegerField | IntegerField |
SlugField | CharField |
SmallIntegerField | IntegerField |
TextField | CharField with widget=Textarea |
TimeField | TimeField |
URLField | URLField with verify_exists set equal to the model field's verify_exists |
USStateField | CharField with widget=USStateSelect |
XMLField | CharField with widget=Textarea |
Did you Know?
The USStateSelect and USPhoneNumberField fields are not from the standard set of Widget objects. They are from the django.contrib.localflavor.us package. Watch for additional Widgets and other things as more features are added to Django.
When each form field is generated, Django uses the following rules to set attributes on it:
If the model field has blank=True, required is set to False on the form field. Otherwise, required defaults to True.
The label attribute of the form field is set to the value of the verbose_name attribute of the model field. The first character is uppercase.
The help_text attribute of the form field is set to the value of the help_text attribute of the model field.
The Select and MultiSelect fields of the form have the same choices set as the choices listed in the model.