257
257
In the `built-in Field classes`_ section below, each ``Field`` defines the
258
258
error message keys it uses.
263
.. versionadded:: 1.2
265
.. attribute:: Field.validators
267
The ``validators`` argument lets you provide a list of validation functions
270
See the :ref:`validators documentation <ref-validators>` for more information.
275
.. versionadded:: 1.2
277
.. attribute:: Field.localize
279
The ``localize`` argument enables the localization of form data, input as well
280
as the rendered output.
282
See the :ref:`format localization <format-localization>` documentation for
260
286
Built-in ``Field`` classes
261
287
--------------------------
643
674
``error_messages`` argument, passing a dictionary with ``'invalid'`` as a key
644
675
and the error message as the value.
680
.. class:: SlugField(**kwargs)
682
* Default widget: ``TextInput``
683
* Empty value: ``''`` (an empty string)
684
* Normalizes to: A Unicode object.
685
* Validates that the given value contains only letters, numbers and
687
* Error messages: ``required``, ``invalid``
689
This field is intended for use in representing a model
690
:class:`~django.db.models.SlugField` in forms.
696
741
String used as the user-agent used when checking for a URL's existence.
697
742
Defaults to the value of the ``URL_VALIDATOR_USER_AGENT`` setting.
744
.. versionchanged:: 1.2
745
The URLField previously did not recognize URLs as valid that contained an IDN
746
(Internationalized Domain Name; a domain name containing unicode characters)
747
domain name. This has now been corrected.
699
750
Slightly complex built-in ``Field`` classes
700
751
-------------------------------------------
702
The following are not yet documented.
704
756
.. class:: ComboField(**kwargs)
758
* Default widget: ``TextInput``
759
* Empty value: ``''`` (an empty string)
760
* Normalizes to: A Unicode object.
761
* Validates that the given value against each of the fields specified
762
as an argument to the ``ComboField``.
763
* Error message keys: ``required``, ``invalid``
765
Takes one extra required argument:
767
.. attribute:: ComboField.fields
769
The list of fields that should be used to validate the field's value (in
770
the order in which they are provided).
772
>>> f = ComboField(fields=[CharField(max_length=20), EmailField()])
773
>>> f.clean('test@example.com')
775
>>> f.clean('longemailaddress@example.com')
776
Traceback (most recent call last):
778
ValidationError: [u'Ensure this value has at most 20 characters (it has 28).']
706
783
.. class:: MultiValueField(**kwargs)
785
* Default widget: ``TextInput``
786
* Empty value: ``''`` (an empty string)
787
* Normalizes to: the type returned by the ``compress`` method of the subclass.
788
* Validates that the given value against each of the fields specified
789
as an argument to the ``MultiValueField``.
790
* Error message keys: ``required``, ``invalid``
792
This abstract field (must be subclassed) aggregates the logic of multiple
793
fields. Subclasses should not have to implement clean(). Instead, they must
794
implement compress(), which takes a list of valid values and returns a
795
"compressed" version of those values -- a single value. For example,
796
:class:`SplitDateTimeField` is a subclass which combines a time field and
797
a date field into a datetime object.
799
Takes one extra required argument:
801
.. attribute:: MultiValueField.fields
803
A list of fields which are cleaned into a single field. Each value in
804
``clean`` is cleaned by the corresponding field in ``fields`` -- the first
805
value is cleaned by the first field, the second value is cleaned by
806
the second field, etc. Once all fields are cleaned, the list of clean
807
values is "compressed" into a single value.
809
``SplitDateTimeField``
810
~~~~~~~~~~~~~~~~~~~~~~
708
812
.. class:: SplitDateTimeField(**kwargs)
710
814
* Default widget: ``SplitDateTimeWidget``
740
844
Fields which handle relationships
741
845
---------------------------------
743
For representing relationships between models, two fields are
744
provided which can derive their choices from a ``QuerySet``:
847
Two fields are available for representing relationships between
848
models: :class:`ModelChoiceField` and
849
:class:`ModelMultipleChoiceField`. Both of these fields require a
850
single ``queryset`` parameter that is used to create the choices for
851
the field. Upon form validation, these fields will place either one
852
model object (in the case of ``ModelChoiceField``) or multiple model
853
objects (in the case of ``ModelMultipleChoiceField``) into the
854
``cleaned_data`` dictionary of the form.
746
859
.. class:: ModelChoiceField(**kwargs)
747
.. class:: ModelMultipleChoiceField(**kwargs)
749
These fields place one or more model objects into the ``cleaned_data``
750
dictionary of forms in which they're used. Both of these fields have an
751
additional required argument:
861
Allows the selection of a single model object, suitable for
862
representing a foreign key. A single argument is required:
753
864
.. attribute:: ModelChoiceField.queryset
756
867
field will be derived, and which will be used to validate the
757
868
user's selection.
762
Allows the selection of a single model object, suitable for
763
representing a foreign key.
870
``ModelChoiceField`` also takes one optional argument:
872
.. attribute:: ModelChoiceField.empty_label
874
By default the ``<select>`` widget used by ``ModelChoiceField`` will have a
875
an empty choice at the top of the list. You can change the text of this
876
label (which is ``"---------"`` by default) with the ``empty_label``
877
attribute, or you can disable the empty label entirely by setting
878
``empty_label`` to ``None``::
880
# A custom empty label
881
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
884
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
886
Note that if a ``ModelChoiceField`` is required and has a default
887
initial value, no empty choice is created (regardless of the value
765
890
The ``__unicode__`` method of the model will be called to generate
766
891
string representations of the objects for use in the field's choices;
773
898
def label_from_instance(self, obj):
774
899
return "My Object #%i" % obj.id
776
.. attribute:: ModelChoiceField.empty_label
778
By default the ``<select>`` widget used by ``ModelChoiceField`` will have a
779
an empty choice at the top of the list. You can change the text of this label
780
(which is ``"---------"`` by default) with the ``empty_label`` attribute, or
781
you can disable the empty label entirely by setting ``empty_label`` to
784
# A custom empty label
785
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
788
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
790
Note that if a ``ModelChoiceField`` is required and has a default
791
initial value, no empty choice is created (regardless of the value
794
901
``ModelMultipleChoiceField``
795
902
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
904
.. class:: ModelMultipleChoiceField(**kwargs)
797
906
Allows the selection of one or more model objects, suitable for
798
representing a many-to-many relation. As with ``ModelChoiceField``,
907
representing a many-to-many relation. As with :class:`ModelChoiceField`,
799
908
you can use ``label_from_instance`` to customize the object
909
representations, and ``queryset`` is a required parameter:
911
.. attribute:: ModelMultipleChoiceField.queryset
913
A ``QuerySet`` of model objects from which the choices for the
914
field will be derived, and which will be used to validate the
802
917
Creating custom fields
803
918
----------------------