django_auxilium.forms.multiple_values module¶
-
class
django_auxilium.forms.multiple_values.
MultipleValuesCharField
(delimiter=u', ', separator=u', ', mapping=None, max_values=None, min_values=None, strip=True, disregard_empty=True, invalid_values=None, *args, **kwargs)[source]¶ Bases:
django.forms.fields.CharField
Form field to allow users to enter multiple values.
The best approach to enter multiple values is to provide a user with multiple input fields however sometimes that is not feasible. In that case this field provides a way for a user to enter multiple values in a single input field. The values can be delimited by either a constant character(s) or even by a regular expression.
Examples
MultipleValuesCharField(delimiter=',') MultipleValuesCharField(delimiter=',', min_values=1, max_values=10) MultipleValuesCharField(delimiter=re.compile(r'\W+'), separator='\n') MultipleValuesCharField(mapping=int) MultipleValuesCharField(mapping=lambda i: i == 5) MultipleValuesCharField(mapping={'foo': 'bar'})
Parameters: - delimiter (str, Regex, optional) – The delimiter according to which the values are split. It can also be given as a
compiled regular expression (e.g.
re.compile('\W+')
). Default is','
(comma). - separator (str) – Iff the delimiter is a regular expression, then this value will be used to separate values within the widget when rendering values back in the UI.
- mapping (dict, callable, optional) – By default all split values are casted to a Python string. The mapping allows to
change that so that individual values will be mapped to different data-types.
Mapping can be defined as a callable which will should accept one parameter, the
value, and return the input value properly casted. Mapping can also be defined
as a dictionary. In that case, if the individual value exists as a key of the
dictionary, the value associated with the key will be used as a final casted
value. Please note that if the key is not found in the dictionary, then the input
value will be used.
Default is
None
. - max_values (int, optional) – The maximum allowed number of values. Default is
None
. - min_values (int, optional) – The minimum required number of provided values. Default is
None
. - strip (bool, optional) – If
True
, then once the user string is split, all values are stripped of whitespace on either side of the string before being converted to Python value by using Python’s stringstrip()
. Default isTrue
. - disregard_empty (bool, optional) – If
True
, then once input string is split, all false evaluated values are disregarded. Default isTrue
. - invalid_values (list, optional) – If provided, this list determines which values are invalid and if any are
encountered, a
ValidationError
will be raised. Useful to blacklist specific values for being validated. If more complex logic is required, please consider usingmapping
as a callable function.
-
default_error_messages
= {u'invalid_value': u'{0} is an invalid value.', u'invalid_values': u'{0} are invalid values.', u'max_values': u'Ensure this value has at least {0} values (it has {1}).', u'min_values': u'Ensure this value has at most {0} values (it has {1}).'}¶
-
prepare_value
(values)[source]¶ Prepare values to be displayed in the UI.
By default this method joins all the values by the
separator
if the values is a list or tuple. Otherwise, this method returns the value itself. Having this method does not require to define a custom widget for this form field.
- delimiter (str, Regex, optional) – The delimiter according to which the values are split. It can also be given as a
compiled regular expression (e.g.