class MultiValueField(fields=(), **kwargs)
[source]
- Default widget:
TextInput
- Empty value:
''
(an empty string) - Normalizes to: the type returned by the
compress
method 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 arequired
validation error will be raised if no value is supplied for any field.When set to
False
, theField.required
attribute can be set toFalse
for individual fields to make them optional. If no value is supplied for a required field, anincomplete
validation error will be raised.A default
incomplete
error message can be defined on theMultiValueField
subclass, 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,
SplitDateTimeField
is a subclass which combines a time field and a date field into adatetime
object.This method must be implemented in the subclasses.
Please login to continue.