djangodocs.org

663 sections in Django 5.0 Search all versions →

MultipleChoiceField

Reference Form fields Built-in Field classes

classMultipleChoiceField(**kwargs) : - 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.

TimeField

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

TypedMultipleChoiceField

Reference Form fields Built-in Field classes

classTypedMultipleChoiceField(**kwargs) : Just like a MultipleChoiceField, except TypedMultipleChoiceField takes two extra arguments, coerce and empty_value.

UUIDField

Reference Form fields Built-in Field classes

classUUIDField(**kwargs) : - Default widget: TextInput - Empty value: None - Normalizes to: A UUID object. - Error message keys: required, invalid

SplitDateTimeField

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

ModelMultipleChoiceField

Reference Form fields Fields which handle relationships

classModelMultipleChoiceField(**kwargs) : - 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.

ModelChoiceIterator

Reference Form fields Fields which handle relationshipsIterating relationship choices

classModelChoiceIterator(field) : The default class assigned to the iterator attribute of ModelChoiceField and ModelMultipleChoiceField. An iterable that yields 2-tuple choices from the queryset.

ModelChoiceIteratorValue

Reference Form fields Fields which handle relationshipsIterating relationship choices

classModelChoiceIteratorValue(value,instance) : Two arguments are required:

Creating custom fields

Reference Form fields

To do this, create a subclass of django.forms.Field. Its only requirements are that it implement a clean() method and that its __init__() method accept the core arguments mentioned above (required, label, initial, widget, help_text).

extend

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.

Paths as strings

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.

Paths as objects

Topic guide Form Assets (the Media class) Paths in asset definitions

Asset paths may also be given as hashable objects implementing an __html__() method. The __html__() method is typically added using the html_safe() decorator. The object is responsible for outputting the complete HTML <script> or <link> tag content:

inlineformset_factory

Reference Model Form Functions

inlineformset_factory(parent_model,model,form=ModelForm,formset=BaseInlineFormSet,fk_name=None,fields=None,exclude=None,extra=3,can_order=False,can_delete=True,max_num=None,formfield_callback=None,widgets=None,validate_max=False,localized_fields=None,labels=None,help_texts=None,error_messages=None,min_num=None,validate_min=False,field_classes=None,absolute_max=None,can_delete_extra=True,renderer=None,edit_only=False) : Returns an InlineFormSet using modelformset_factory() with defaults of formset=BaseInlineFormSet, can_delete=True, and extra=3.