Reference
Form fields
Built-in Field classes
classMultipleChoiceField(**kwargs)[source] : - Default widget: SelectMultiple - Empty value: [] (an empty list) - Normalizes to: A list of strings. - Validates that every value in the given list of values exists in the list of choices.
Reference
Form fields
Built-in Field classes
- Validates that the given value is either a datetime.time or string formatted in a particular time format. - Error message keys: required, invalid
Reference
Form fields
Built-in Field classes
classTypedMultipleChoiceField(**kwargs)[source] : Just like a MultipleChoiceField, except TypedMultipleChoiceField takes two extra arguments, coerce and empty_value.
Reference
Form fields
Built-in Field classes
classUUIDField(**kwargs)[source] : - Default widget: TextInput - Empty value: None - Normalizes to: A UUID object. - Error message keys: required, invalid
Reference
Form fields
Reference
Form fields
Slightly complex built-in Field classes
- Validates that the given value is a datetime.datetime or string formatted in a particular datetime format. - Error message keys: required, invalid, invalid_date, invalid_time
Reference
Form fields
Fields which handle relationships
classModelMultipleChoiceField(**kwargs)[source] : - Default widget: SelectMultiple - Empty value: An empty QuerySet (self.queryset.none()) - Normalizes to: A QuerySet of model instances. - Validates that every id in the given list of values exists in the queryset.
Reference
Form fields
Fields which handle relationships
By default, ModelChoiceField and ModelMultipleChoiceField use ModelChoiceIterator to generate their field choices. When iterated, ModelChoiceIterator yields 2-tuple choices containing ModelChoiceIteratorValue instances as the first value element in each choice.
Reference
Form fields
Fields which handle relationshipsIterating relationship choices
classModelChoiceIterator(field)[source] : The default class assigned to the iterator attribute of ModelChoiceField and ModelMultipleChoiceField. An iterable that yields 2-tuple choices from the queryset.
Reference
Form fields
Fields which handle relationshipsIterating relationship choices
classModelChoiceIteratorValue(value,instance)[source] : Two arguments are required:
Topic guide
Form Assets (the Media class)
Assets as a static definition
A boolean defining inheritance behavior for Media declarations. By default, any object using a static Media definition will inherit all the assets associated with the parent widget. This occurs regardless of how the parent defines its own requirements.
Topic guide
Form Assets (the Media class)
If you need to perform some more sophisticated manipulation of asset requirements, you can define the media property directly. This is done by defining a widget property that returns an instance of forms.Media.
Topic guide
Form Assets (the Media class)
Topic guide
Form Assets (the Media class)
Paths in asset definitions
String paths used to specify assets can be either relative or absolute. If a path starts with /, http:// or https://, it will be interpreted as an absolute path, and left as-is.
Topic guide
Form Assets (the Media class)
Paths in asset definitions
Assets may also be object-based, using Script. Furthermore, these allow you to pass custom HTML attributes: If this Media definition were to be rendered, it would become the following HTML: Changed in Django 5.2: The object class Script was added.
Topic guide
Form Assets (the Media class)
Media objects
If you only want files of a particular type, you can use the subscript operator to filter out a medium of interest.
Topic guide
Form Assets (the Media class)
Media objects
Media objects can also be added together. When two Media objects are added, the resulting Media object contains the union of the assets specified by both:
Topic guide
Form Assets (the Media class)
Media objects
The order in which assets are inserted into the DOM is often important. For example, you may have a script that depends on jQuery.
Reference
The form rendering API
Built-in-template form renderers
Templates for the built-in widgets are located in django/forms/jinja2 and installed apps can provide templates in a jinja2 directory. To use this backend, all the forms and widgets in your project and its third-party apps must have Jinja2 templates.
Reference
The form rendering API
Formset templates receive a context from BaseFormSet.get_context(). By default, formsets receive a dictionary with the following values: formset: The formset instance.