class MultiValueField(fields=(), **kwargs) [source]
- Default widget:
TextInput - Empty value:
''(an empty string) - Normalizes to: the type returned by the
compressmethod of the subclass. - Validates the given value against each of the fields specified as an argument to the
MultiValueField. - Error message keys:
required,invalid,incomplete
Aggregates the logic of multiple fields that together produce a single value.
This field is abstract and must be subclassed. In contrast with the single-value fields, subclasses of MultiValueField must not implement clean() but instead - implement compress().
Takes one extra required argument:
-
fields -
A tuple of fields whose values are cleaned and subsequently combined into a single value. Each value of the field is cleaned by the corresponding field in
fields– the first value is cleaned by the first field, the second value is cleaned by the second field, etc. Once all fields are cleaned, the list of clean values is combined into a single value bycompress().
Also takes one extra optional argument:
-
require_all_fields -
Defaults to
True, in which case arequiredvalidation error will be raised if no value is supplied for any field.When set to
False, theField.requiredattribute can be set toFalsefor individual fields to make them optional. If no value is supplied for a required field, anincompletevalidation error will be raised.A default
incompleteerror message can be defined on theMultiValueFieldsubclass, or different messages can be defined on each individual field. For example:from django.core.validators import RegexValidator class PhoneField(MultiValueField): def __init__(self, *args, **kwargs): # Define one message for all fields. error_messages = { 'incomplete': 'Enter a country calling code and a phone number.', } # Or define a different message for each field. fields = ( CharField( error_messages={'incomplete': 'Enter a country calling code.'}, validators=[ RegexValidator(r'^[0-9]+$', 'Enter a valid country calling code.'), ], ), CharField( error_messages={'incomplete': 'Enter a phone number.'}, validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid phone number.')], ), CharField( validators=[RegexValidator(r'^[0-9]+$', 'Enter a valid extension.')], required=False, ), ) super(PhoneField, self).__init__( error_messages=error_messages, fields=fields, require_all_fields=False, *args, **kwargs )
-
widget -
Must be a subclass of
django.forms.MultiWidget. Default value isTextInput, which probably is not very useful in this case.
-
compress(data_list)[source] -
Takes a list of valid values and returns a “compressed” version of those values – in a single value. For example,
SplitDateTimeFieldis a subclass which combines a time field and a date field into adatetimeobject.This method must be implemented in the subclasses.
Please login to continue.