2008-08-24 06:25:40 +08:00
|
|
|
===========
|
|
|
|
Form fields
|
|
|
|
===========
|
|
|
|
|
|
|
|
.. module:: django.forms.fields
|
|
|
|
:synopsis: Django's built-in form fields.
|
|
|
|
|
|
|
|
.. currentmodule:: django.forms
|
|
|
|
|
|
|
|
.. class:: Field(**kwargs)
|
|
|
|
|
|
|
|
When you create a ``Form`` class, the most important part is defining the
|
|
|
|
fields of the form. Each field has custom validation logic, along with a few
|
|
|
|
other hooks.
|
|
|
|
|
|
|
|
.. method:: Field.clean(value)
|
|
|
|
|
|
|
|
Although the primary way you'll use ``Field`` classes is in ``Form`` classes,
|
|
|
|
you can also instantiate them and use them directly to get a better idea of
|
|
|
|
how they work. Each ``Field`` instance has a ``clean()`` method, which takes
|
|
|
|
a single argument and either raises a ``django.forms.ValidationError``
|
|
|
|
exception or returns the clean value::
|
|
|
|
|
2009-02-21 16:45:47 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f = forms.EmailField()
|
|
|
|
>>> f.clean('foo@example.com')
|
2014-03-23 04:30:49 +08:00
|
|
|
'foo@example.com'
|
2011-04-02 00:10:22 +08:00
|
|
|
>>> f.clean('invalid email address')
|
2008-08-24 06:25:40 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['Enter a valid email address.']
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
.. _core-field-arguments:
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Core field arguments
|
2016-01-03 18:56:22 +08:00
|
|
|
====================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Each ``Field`` class constructor takes at least these arguments. Some
|
|
|
|
``Field`` classes take additional, field-specific arguments, but the following
|
|
|
|
should *always* be accepted:
|
|
|
|
|
|
|
|
``required``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.required
|
|
|
|
|
|
|
|
By default, each ``Field`` class assumes the value is required, so if you pass
|
|
|
|
an empty value -- either ``None`` or the empty string (``""``) -- then
|
|
|
|
``clean()`` will raise a ``ValidationError`` exception::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f = forms.CharField()
|
|
|
|
>>> f.clean('foo')
|
2014-03-23 04:30:49 +08:00
|
|
|
'foo'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean('')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['This field is required.']
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(None)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['This field is required.']
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(' ')
|
2014-03-23 04:30:49 +08:00
|
|
|
' '
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(0)
|
2014-03-23 04:30:49 +08:00
|
|
|
'0'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(True)
|
2014-03-23 04:30:49 +08:00
|
|
|
'True'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(False)
|
2014-03-23 04:30:49 +08:00
|
|
|
'False'
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
To specify that a field is *not* required, pass ``required=False`` to the
|
|
|
|
``Field`` constructor::
|
|
|
|
|
|
|
|
>>> f = forms.CharField(required=False)
|
|
|
|
>>> f.clean('foo')
|
2014-03-23 04:30:49 +08:00
|
|
|
'foo'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean('')
|
2014-03-23 04:30:49 +08:00
|
|
|
''
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(None)
|
2014-03-23 04:30:49 +08:00
|
|
|
''
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(0)
|
2014-03-23 04:30:49 +08:00
|
|
|
'0'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(True)
|
2014-03-23 04:30:49 +08:00
|
|
|
'True'
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> f.clean(False)
|
2014-03-23 04:30:49 +08:00
|
|
|
'False'
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
If a ``Field`` has ``required=False`` and you pass ``clean()`` an empty value,
|
|
|
|
then ``clean()`` will return a *normalized* empty value rather than raising
|
2017-01-21 05:04:05 +08:00
|
|
|
``ValidationError``. For ``CharField``, this will be an empty string. For other
|
|
|
|
``Field`` classes, it might be ``None``. (This varies from field to field.)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2017-01-14 18:36:52 +08:00
|
|
|
Widgets of required form fields have the ``required`` HTML attribute. Set the
|
|
|
|
:attr:`Form.use_required_attribute` attribute to ``False`` to disable it. The
|
|
|
|
``required`` attribute isn't included on forms of formsets because the browser
|
|
|
|
validation may not be correct when adding and deleting formsets.
|
|
|
|
|
|
|
|
.. versionadded:: 1.10
|
|
|
|
|
|
|
|
Support for the ``required`` HTML attribute was added.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``label``
|
2016-01-03 18:56:22 +08:00
|
|
|
---------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.label
|
|
|
|
|
|
|
|
The ``label`` argument lets you specify the "human-friendly" label for this
|
|
|
|
field. This is used when the ``Field`` is displayed in a ``Form``.
|
|
|
|
|
|
|
|
As explained in "Outputting forms as HTML" above, the default label for a
|
|
|
|
``Field`` is generated from the field name by converting all underscores to
|
|
|
|
spaces and upper-casing the first letter. Specify ``label`` if that default
|
|
|
|
behavior doesn't result in an adequate label.
|
|
|
|
|
|
|
|
Here's a full example ``Form`` that implements ``label`` for two of its fields.
|
|
|
|
We've specified ``auto_id=False`` to simplify the output::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> class CommentForm(forms.Form):
|
|
|
|
... name = forms.CharField(label='Your name')
|
2015-11-15 20:05:15 +08:00
|
|
|
... url = forms.URLField(label='Your website', required=False)
|
2008-08-24 06:25:40 +08:00
|
|
|
... comment = forms.CharField()
|
|
|
|
>>> f = CommentForm(auto_id=False)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f)
|
2016-03-29 02:02:04 +08:00
|
|
|
<tr><th>Your name:</th><td><input type="text" name="name" required /></td></tr>
|
|
|
|
<tr><th>Your website:</th><td><input type="url" name="url" required /></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" required /></td></tr>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-04-29 16:07:57 +08:00
|
|
|
``label_suffix``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------
|
2014-04-29 16:07:57 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.label_suffix
|
|
|
|
|
|
|
|
The ``label_suffix`` argument lets you override the form's
|
|
|
|
:attr:`~django.forms.Form.label_suffix` on a per-field basis::
|
|
|
|
|
|
|
|
>>> class ContactForm(forms.Form):
|
|
|
|
... age = forms.IntegerField()
|
|
|
|
... nationality = forms.CharField()
|
|
|
|
... captcha_answer = forms.IntegerField(label='2 + 2', label_suffix=' =')
|
|
|
|
>>> f = ContactForm(label_suffix='?')
|
|
|
|
>>> print(f.as_p())
|
2016-03-29 02:02:04 +08:00
|
|
|
<p><label for="id_age">Age?</label> <input id="id_age" name="age" type="number" required /></p>
|
|
|
|
<p><label for="id_nationality">Nationality?</label> <input id="id_nationality" name="nationality" type="text" required /></p>
|
|
|
|
<p><label for="id_captcha_answer">2 + 2 =</label> <input id="id_captcha_answer" name="captcha_answer" type="number" required /></p>
|
2014-04-29 16:07:57 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``initial``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.initial
|
|
|
|
|
|
|
|
The ``initial`` argument lets you specify the initial value to use when
|
|
|
|
rendering this ``Field`` in an unbound ``Form``.
|
|
|
|
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
To specify dynamic initial data, see the :attr:`Form.initial` parameter.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
The use-case for this is when you want to display an "empty" form in which a
|
|
|
|
field is initialized to a particular value. For example::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> class CommentForm(forms.Form):
|
|
|
|
... name = forms.CharField(initial='Your name')
|
|
|
|
... url = forms.URLField(initial='http://')
|
|
|
|
... comment = forms.CharField()
|
|
|
|
>>> f = CommentForm(auto_id=False)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f)
|
2016-03-29 02:02:04 +08:00
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" value="Your name" required /></td></tr>
|
|
|
|
<tr><th>Url:</th><td><input type="url" name="url" value="http://" required /></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><input type="text" name="comment" required /></td></tr>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
You may be thinking, why not just pass a dictionary of the initial values as
|
|
|
|
data when displaying the form? Well, if you do that, you'll trigger validation,
|
|
|
|
and the HTML output will include any validation errors::
|
|
|
|
|
|
|
|
>>> class CommentForm(forms.Form):
|
|
|
|
... name = forms.CharField()
|
|
|
|
... url = forms.URLField()
|
|
|
|
... comment = forms.CharField()
|
|
|
|
>>> default_data = {'name': 'Your name', 'url': 'http://'}
|
|
|
|
>>> f = CommentForm(default_data, auto_id=False)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f)
|
2016-03-29 02:02:04 +08:00
|
|
|
<tr><th>Name:</th><td><input type="text" name="name" value="Your name" required /></td></tr>
|
|
|
|
<tr><th>Url:</th><td><ul class="errorlist"><li>Enter a valid URL.</li></ul><input type="url" name="url" value="http://" required /></td></tr>
|
|
|
|
<tr><th>Comment:</th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="comment" required /></td></tr>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
This is why ``initial`` values are only displayed for unbound forms. For bound
|
|
|
|
forms, the HTML output will use the bound data.
|
|
|
|
|
|
|
|
Also note that ``initial`` values are *not* used as "fallback" data in
|
|
|
|
validation if a particular field's value is not given. ``initial`` values are
|
|
|
|
*only* intended for initial form display::
|
|
|
|
|
|
|
|
>>> class CommentForm(forms.Form):
|
|
|
|
... name = forms.CharField(initial='Your name')
|
|
|
|
... url = forms.URLField(initial='http://')
|
|
|
|
... comment = forms.CharField()
|
|
|
|
>>> data = {'name': '', 'url': '', 'comment': 'Foo'}
|
|
|
|
>>> f = CommentForm(data)
|
|
|
|
>>> f.is_valid()
|
|
|
|
False
|
|
|
|
# The form does *not* fall back to using the initial values.
|
|
|
|
>>> f.errors
|
2014-03-23 04:30:49 +08:00
|
|
|
{'url': ['This field is required.'], 'name': ['This field is required.']}
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2009-04-01 07:34:03 +08:00
|
|
|
Instead of a constant, you can also pass any callable::
|
|
|
|
|
|
|
|
>>> import datetime
|
|
|
|
>>> class DateForm(forms.Form):
|
|
|
|
... day = forms.DateField(initial=datetime.date.today)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(DateForm())
|
2016-03-29 02:02:04 +08:00
|
|
|
<tr><th>Day:</th><td><input type="text" name="day" value="12/23/2008" required /><td></tr>
|
2009-04-01 07:34:03 +08:00
|
|
|
|
|
|
|
The callable will be evaluated only when the unbound form is displayed, not when it is defined.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``widget``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.widget
|
|
|
|
|
|
|
|
The ``widget`` argument lets you specify a ``Widget`` class to use when
|
2010-08-20 03:27:44 +08:00
|
|
|
rendering this ``Field``. See :doc:`/ref/forms/widgets` for more information.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``help_text``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.help_text
|
|
|
|
|
|
|
|
The ``help_text`` argument lets you specify descriptive text for this
|
|
|
|
``Field``. If you provide ``help_text``, it will be displayed next to the
|
|
|
|
``Field`` when the ``Field`` is rendered by one of the convenience ``Form``
|
|
|
|
methods (e.g., ``as_ul()``).
|
|
|
|
|
2015-11-24 23:12:42 +08:00
|
|
|
Like the model field's :attr:`~django.db.models.Field.help_text`, this value
|
|
|
|
isn't HTML-escaped in automatically-generated forms.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Here's a full example ``Form`` that implements ``help_text`` for two of its
|
|
|
|
fields. We've specified ``auto_id=False`` to simplify the output::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> class HelpTextContactForm(forms.Form):
|
|
|
|
... subject = forms.CharField(max_length=100, help_text='100 characters max.')
|
|
|
|
... message = forms.CharField()
|
2011-04-02 00:10:22 +08:00
|
|
|
... sender = forms.EmailField(help_text='A valid email address, please.')
|
2008-08-24 06:25:40 +08:00
|
|
|
... cc_myself = forms.BooleanField(required=False)
|
|
|
|
>>> f = HelpTextContactForm(auto_id=False)
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f.as_table())
|
2016-03-29 02:02:04 +08:00
|
|
|
<tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" required /><br /><span class="helptext">100 characters max.</span></td></tr>
|
|
|
|
<tr><th>Message:</th><td><input type="text" name="message" required /></td></tr>
|
|
|
|
<tr><th>Sender:</th><td><input type="email" name="sender" required /><br />A valid email address, please.</td></tr>
|
2008-08-24 06:25:40 +08:00
|
|
|
<tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr>
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f.as_ul()))
|
2016-03-29 02:02:04 +08:00
|
|
|
<li>Subject: <input type="text" name="subject" maxlength="100" required /> <span class="helptext">100 characters max.</span></li>
|
|
|
|
<li>Message: <input type="text" name="message" required /></li>
|
|
|
|
<li>Sender: <input type="email" name="sender" required /> A valid email address, please.</li>
|
2008-08-24 06:25:40 +08:00
|
|
|
<li>Cc myself: <input type="checkbox" name="cc_myself" /></li>
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(f.as_p())
|
2016-03-29 02:02:04 +08:00
|
|
|
<p>Subject: <input type="text" name="subject" maxlength="100" required /> <span class="helptext">100 characters max.</span></p>
|
|
|
|
<p>Message: <input type="text" name="message" required /></p>
|
|
|
|
<p>Sender: <input type="email" name="sender" required /> A valid email address, please.</p>
|
2008-08-24 06:25:40 +08:00
|
|
|
<p>Cc myself: <input type="checkbox" name="cc_myself" /></p>
|
|
|
|
|
|
|
|
``error_messages``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.error_messages
|
|
|
|
|
|
|
|
The ``error_messages`` argument lets you override the default messages that the
|
|
|
|
field will raise. Pass in a dictionary with keys matching the error messages you
|
|
|
|
want to override. For example, here is the default error message::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2008-08-24 06:25:40 +08:00
|
|
|
>>> generic = forms.CharField()
|
|
|
|
>>> generic.clean('')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['This field is required.']
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
And here is a custom error message::
|
|
|
|
|
|
|
|
>>> name = forms.CharField(error_messages={'required': 'Please enter your name'})
|
|
|
|
>>> name.clean('')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['Please enter your name']
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
In the `built-in Field classes`_ section below, each ``Field`` defines the
|
|
|
|
error message keys it uses.
|
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
``validators``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2010-03-28 00:43:27 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
.. attribute:: Field.validators
|
|
|
|
|
|
|
|
The ``validators`` argument lets you provide a list of validation functions
|
|
|
|
for this field.
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
See the :doc:`validators documentation </ref/validators>` for more information.
|
2010-01-05 11:56:19 +08:00
|
|
|
|
2010-03-28 00:43:27 +08:00
|
|
|
``localize``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------
|
2010-03-28 00:43:27 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.localize
|
|
|
|
|
2015-05-25 22:13:01 +08:00
|
|
|
The ``localize`` argument enables the localization of form data input, as well
|
2010-03-28 00:43:27 +08:00
|
|
|
as the rendered output.
|
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
See the :doc:`format localization </topics/i18n/formatting>` documentation for
|
2010-03-28 00:43:27 +08:00
|
|
|
more information.
|
|
|
|
|
2015-07-08 03:06:57 +08:00
|
|
|
``disabled``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------
|
2015-07-08 03:06:57 +08:00
|
|
|
|
|
|
|
.. attribute:: Field.disabled
|
|
|
|
|
|
|
|
The ``disabled`` boolean argument, when set to ``True``, disables a form field
|
|
|
|
using the ``disabled`` HTML attribute so that it won't be editable by users.
|
|
|
|
Even if a user tampers with the field's value submitted to the server, it will
|
|
|
|
be ignored in favor of the value from the form's initial data.
|
2014-08-07 10:56:23 +08:00
|
|
|
|
|
|
|
Checking if the field data has changed
|
2016-01-03 18:56:22 +08:00
|
|
|
======================================
|
2014-08-07 10:56:23 +08:00
|
|
|
|
|
|
|
``has_changed()``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2014-08-07 10:56:23 +08:00
|
|
|
|
|
|
|
.. method:: Field.has_changed()
|
|
|
|
|
|
|
|
The ``has_changed()`` method is used to determine if the field value has changed
|
|
|
|
from the initial value. Returns ``True`` or ``False``.
|
|
|
|
|
|
|
|
See the :class:`Form.has_changed()` documentation for more information.
|
|
|
|
|
2014-08-04 19:29:59 +08:00
|
|
|
.. _built-in-fields:
|
2010-03-28 00:43:27 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Built-in ``Field`` classes
|
2016-01-03 18:56:22 +08:00
|
|
|
==========================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Naturally, the ``forms`` library comes with a set of ``Field`` classes that
|
|
|
|
represent common validation needs. This section documents each built-in field.
|
|
|
|
|
|
|
|
For each field, we describe the default widget used if you don't specify
|
|
|
|
``widget``. We also specify the value returned when you provide an empty value
|
|
|
|
(see the section on ``required`` above to understand what that means).
|
|
|
|
|
|
|
|
``BooleanField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: BooleanField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`CheckboxInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``False``
|
|
|
|
* Normalizes to: A Python ``True`` or ``False`` value.
|
2009-07-15 21:54:11 +08:00
|
|
|
* Validates that the value is ``True`` (e.g. the check box is checked) if
|
2008-08-24 06:25:40 +08:00
|
|
|
the field has ``required=True``.
|
|
|
|
* Error message keys: ``required``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. note::
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Since all ``Field`` subclasses have ``required=True`` by default, the
|
|
|
|
validation condition here is important. If you want to include a boolean
|
|
|
|
in your form that can be either ``True`` or ``False`` (e.g. a checked or
|
|
|
|
unchecked checkbox), you must remember to pass in ``required=False`` when
|
|
|
|
creating the ``BooleanField``.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``CharField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: CharField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2016-05-18 22:30:42 +08:00
|
|
|
* Empty value: Whatever you've given as :attr:`empty_value`.
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates ``max_length`` or ``min_length``, if they are provided.
|
|
|
|
Otherwise, all inputs are valid.
|
|
|
|
* Error message keys: ``required``, ``max_length``, ``min_length``
|
|
|
|
|
2015-02-05 20:26:15 +08:00
|
|
|
Has three optional arguments for validation:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: max_length
|
|
|
|
.. attribute:: min_length
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
If provided, these arguments ensure that the string is at most or at least
|
|
|
|
the given length.
|
|
|
|
|
2015-02-05 20:26:15 +08:00
|
|
|
.. attribute:: strip
|
|
|
|
|
|
|
|
If ``True`` (default), the value will be stripped of leading and
|
|
|
|
trailing whitespace.
|
|
|
|
|
2016-05-18 22:30:42 +08:00
|
|
|
.. attribute:: empty_value
|
|
|
|
|
|
|
|
.. versionadded:: 1.11
|
|
|
|
|
|
|
|
The value to use to represent "empty". Defaults to an empty string.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``ChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
---------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: ChoiceField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`Select`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that the given value exists in the list of choices.
|
|
|
|
* Error message keys: ``required``, ``invalid_choice``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``invalid_choice`` error message may contain ``%(value)s``, which will be
|
|
|
|
replaced with the selected choice.
|
2010-12-29 05:10:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one extra required argument:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: choices
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2014-10-28 04:21:59 +08:00
|
|
|
Either an iterable (e.g., a list or tuple) of 2-tuples to use as
|
|
|
|
choices for this field, or a callable that returns such an iterable.
|
|
|
|
This argument accepts the same formats as the ``choices`` argument to a
|
|
|
|
model field. See the :ref:`model field reference documentation on
|
|
|
|
choices <field-choices>` for more details. If the argument is a
|
|
|
|
callable, it is evaluated each time the field's form is initialized.
|
|
|
|
|
2008-09-01 04:10:50 +08:00
|
|
|
``TypedChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------------
|
2008-09-01 04:10:50 +08:00
|
|
|
|
|
|
|
.. class:: TypedChoiceField(**kwargs)
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Just like a :class:`ChoiceField`, except :class:`TypedChoiceField` takes two
|
2016-06-05 22:54:24 +08:00
|
|
|
extra arguments, :attr:`coerce` and :attr:`empty_value`.
|
2008-09-01 04:10:50 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`Select`
|
2016-06-05 22:54:24 +08:00
|
|
|
* Empty value: Whatever you've given as :attr:`empty_value`.
|
|
|
|
* Normalizes to: A value of the type provided by the :attr:`coerce`
|
|
|
|
argument.
|
2010-12-05 12:47:19 +08:00
|
|
|
* Validates that the given value exists in the list of choices and can be
|
|
|
|
coerced.
|
2008-09-01 04:10:50 +08:00
|
|
|
* Error message keys: ``required``, ``invalid_choice``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes extra arguments:
|
2008-09-01 04:10:50 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: coerce
|
2008-09-01 04:10:50 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A function that takes one argument and returns a coerced value. Examples
|
|
|
|
include the built-in ``int``, ``float``, ``bool`` and other types. Defaults
|
2013-11-19 01:24:56 +08:00
|
|
|
to an identity function. Note that coercion happens after input
|
|
|
|
validation, so it is possible to coerce to a value not present in
|
|
|
|
``choices``.
|
2008-09-01 04:10:50 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: empty_value
|
2008-09-01 04:10:50 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The value to use to represent "empty." Defaults to the empty string;
|
|
|
|
``None`` is another common choice here. Note that this value will not be
|
|
|
|
coerced by the function given in the ``coerce`` argument, so choose it
|
|
|
|
accordingly.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``DateField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: DateField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`DateInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``datetime.date`` object.
|
|
|
|
* Validates that the given value is either a ``datetime.date``,
|
|
|
|
``datetime.datetime`` or string formatted in a particular date format.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one optional argument:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: input_formats
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A list of formats used to attempt to convert a string to a valid
|
|
|
|
``datetime.date`` object.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If no ``input_formats`` argument is provided, the default input formats are::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-08-04 19:29:59 +08:00
|
|
|
['%Y-%m-%d', # '2006-10-25'
|
2015-06-11 21:34:03 +08:00
|
|
|
'%m/%d/%Y', # '10/25/2006'
|
|
|
|
'%m/%d/%y'] # '10/25/06'
|
2012-09-08 07:49:38 +08:00
|
|
|
|
|
|
|
Additionally, if you specify :setting:`USE_L10N=False<USE_L10N>` in your settings, the
|
|
|
|
following will also be included in the default input formats::
|
|
|
|
|
2013-02-26 01:22:02 +08:00
|
|
|
['%b %d %Y', # 'Oct 25 2006'
|
2015-06-11 21:34:03 +08:00
|
|
|
'%b %d, %Y', # 'Oct 25, 2006'
|
|
|
|
'%d %b %Y', # '25 Oct 2006'
|
|
|
|
'%d %b, %Y', # '25 Oct, 2006'
|
|
|
|
'%B %d %Y', # 'October 25 2006'
|
|
|
|
'%B %d, %Y', # 'October 25, 2006'
|
|
|
|
'%d %B %Y', # '25 October 2006'
|
|
|
|
'%d %B, %Y'] # '25 October, 2006'
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
See also :doc:`format localization </topics/i18n/formatting>`.
|
2012-10-13 17:17:25 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``DateTimeField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: DateTimeField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`DateTimeInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``datetime.datetime`` object.
|
|
|
|
* Validates that the given value is either a ``datetime.datetime``,
|
|
|
|
``datetime.date`` or string formatted in a particular datetime format.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one optional argument:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: input_formats
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A list of formats used to attempt to convert a string to a valid
|
|
|
|
``datetime.datetime`` object.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If no ``input_formats`` argument is provided, the default input formats are::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-02-26 01:22:02 +08:00
|
|
|
['%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
|
2015-06-11 21:34:03 +08:00
|
|
|
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
|
|
|
|
'%Y-%m-%d', # '2006-10-25'
|
|
|
|
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
|
|
|
|
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
|
|
|
|
'%m/%d/%Y', # '10/25/2006'
|
|
|
|
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
|
|
|
|
'%m/%d/%y %H:%M', # '10/25/06 14:30'
|
|
|
|
'%m/%d/%y'] # '10/25/06'
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
See also :doc:`format localization </topics/i18n/formatting>`.
|
2012-10-13 17:17:25 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``DecimalField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. class:: DecimalField(**kwargs)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-02-23 16:45:56 +08:00
|
|
|
* Default widget: :class:`NumberInput` when :attr:`Field.localize` is
|
|
|
|
``False``, else :class:`TextInput`.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``decimal``.
|
|
|
|
* Validates that the given value is a decimal. Leading and trailing
|
|
|
|
whitespace is ignored.
|
|
|
|
* Error message keys: ``required``, ``invalid``, ``max_value``,
|
|
|
|
``min_value``, ``max_digits``, ``max_decimal_places``,
|
|
|
|
``max_whole_digits``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``max_value`` and ``min_value`` error messages may contain
|
|
|
|
``%(limit_value)s``, which will be substituted by the appropriate limit.
|
2014-03-24 23:42:56 +08:00
|
|
|
Similarly, the ``max_digits``, ``max_decimal_places`` and
|
|
|
|
``max_whole_digits`` error messages may contain ``%(max)s``.
|
2013-04-19 02:38:07 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes four optional arguments:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: max_value
|
|
|
|
.. attribute:: min_value
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
These control the range of values permitted in the field, and should be
|
|
|
|
given as ``decimal.Decimal`` values.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: max_digits
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The maximum number of digits (those before the decimal point plus those
|
|
|
|
after the decimal point, with leading zeros stripped) permitted in the
|
|
|
|
value.
|
2009-07-15 21:54:11 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: decimal_places
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The maximum number of decimal places permitted.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-07-24 20:57:24 +08:00
|
|
|
``DurationField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2014-07-24 20:57:24 +08:00
|
|
|
|
|
|
|
.. class:: DurationField(**kwargs)
|
|
|
|
|
|
|
|
* Default widget: :class:`TextInput`
|
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python :class:`~python:datetime.timedelta`.
|
|
|
|
* Validates that the given value is a string which can be converted into a
|
|
|
|
``timedelta``.
|
|
|
|
* Error message keys: ``required``, ``invalid``.
|
|
|
|
|
|
|
|
Accepts any format understood by
|
|
|
|
:func:`~django.utils.dateparse.parse_duration`.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``EmailField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: EmailField(**kwargs)
|
|
|
|
|
2013-01-28 21:12:56 +08:00
|
|
|
* Default widget: :class:`EmailInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2011-04-02 00:10:22 +08:00
|
|
|
* Validates that the given value is a valid email address, using a
|
2008-08-24 06:25:40 +08:00
|
|
|
moderately complex regular expression.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Has two optional arguments for validation, ``max_length`` and ``min_length``.
|
|
|
|
If provided, these arguments ensure that the string is at most or at least the
|
|
|
|
given length.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``FileField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. class:: FileField(**kwargs)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`ClearableFileInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: An ``UploadedFile`` object that wraps the file content
|
|
|
|
and file name into a single object.
|
2011-04-23 05:18:27 +08:00
|
|
|
* Can validate that non-empty file data has been bound to the form.
|
2010-12-29 05:10:19 +08:00
|
|
|
* Error message keys: ``required``, ``invalid``, ``missing``, ``empty``,
|
|
|
|
``max_length``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Has two optional arguments for validation, ``max_length`` and
|
|
|
|
``allow_empty_file``. If provided, these ensure that the file name is at
|
|
|
|
most the given length, and that validation will succeed even if the file
|
|
|
|
content is empty.
|
2011-04-23 05:18:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
To learn more about the ``UploadedFile`` object, see the :doc:`file uploads
|
|
|
|
documentation </topics/http/file-uploads>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
When you use a ``FileField`` in a form, you must also remember to
|
|
|
|
:ref:`bind the file data to the form <binding-uploaded-files>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``max_length`` error refers to the length of the filename. In the error
|
|
|
|
message for that key, ``%(max)d`` will be replaced with the maximum filename
|
|
|
|
length and ``%(length)d`` will be replaced with the current filename length.
|
2010-12-29 05:10:19 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``FilePathField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. class:: FilePathField(**kwargs)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`Select`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that the selected choice exists in the list of choices.
|
|
|
|
* Error message keys: ``required``, ``invalid_choice``
|
|
|
|
|
2016-04-03 18:21:37 +08:00
|
|
|
The field allows choosing from files inside a certain directory. It takes five
|
2011-10-14 08:12:01 +08:00
|
|
|
extra arguments; only ``path`` is required:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: path
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The absolute path to the directory whose contents you want listed. This
|
|
|
|
directory must exist.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: recursive
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If ``False`` (the default) only the direct contents of ``path`` will be
|
|
|
|
offered as choices. If ``True``, the directory will be descended into
|
|
|
|
recursively and all descendants will be listed as choices.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: match
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A regular expression pattern; only files with names matching this expression
|
|
|
|
will be allowed as choices.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-04-22 22:44:08 +08:00
|
|
|
.. attribute:: allow_files
|
|
|
|
|
|
|
|
Optional. Either ``True`` or ``False``. Default is ``True``. Specifies
|
|
|
|
whether files in the specified location should be included. Either this or
|
|
|
|
:attr:`allow_folders` must be ``True``.
|
|
|
|
|
|
|
|
.. attribute:: allow_folders
|
|
|
|
|
|
|
|
Optional. Either ``True`` or ``False``. Default is ``False``. Specifies
|
|
|
|
whether folders in the specified location should be included. Either this or
|
|
|
|
:attr:`allow_files` must be ``True``.
|
|
|
|
|
|
|
|
|
2009-07-15 21:54:11 +08:00
|
|
|
``FloatField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2009-07-15 21:54:11 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. class:: FloatField(**kwargs)
|
|
|
|
|
2013-02-23 16:45:56 +08:00
|
|
|
* Default widget: :class:`NumberInput` when :attr:`Field.localize` is
|
|
|
|
``False``, else :class:`TextInput`.
|
2009-07-15 21:54:11 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python float.
|
2015-05-25 22:13:01 +08:00
|
|
|
* Validates that the given value is a float. Leading and trailing
|
2009-07-15 21:54:11 +08:00
|
|
|
whitespace is allowed, as in Python's ``float()`` function.
|
|
|
|
* Error message keys: ``required``, ``invalid``, ``max_value``,
|
|
|
|
``min_value``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes two optional arguments for validation, ``max_value`` and ``min_value``.
|
|
|
|
These control the range of values permitted in the field.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``ImageField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2008-09-02 11:40:42 +08:00
|
|
|
.. class:: ImageField(**kwargs)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`ClearableFileInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: An ``UploadedFile`` object that wraps the file content
|
|
|
|
and file name into a single object.
|
|
|
|
* Validates that file data has been bound to the form, and that the
|
2014-03-21 22:54:53 +08:00
|
|
|
file is of an image format understood by Pillow.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Error message keys: ``required``, ``invalid``, ``missing``, ``empty``,
|
|
|
|
``invalid_image``
|
|
|
|
|
2014-03-21 22:54:53 +08:00
|
|
|
Using an ``ImageField`` requires that `Pillow`_ is installed with support
|
|
|
|
for the image formats you use. If you encounter a ``corrupt image`` error
|
|
|
|
when you upload an image, it usually means that Pillow doesn't understand
|
|
|
|
its format. To fix this, install the appropriate library and reinstall
|
|
|
|
Pillow.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
When you use an ``ImageField`` on a form, you must also remember to
|
|
|
|
:ref:`bind the file data to the form <binding-uploaded-files>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-06-16 23:24:59 +08:00
|
|
|
After the field has been cleaned and validated, the ``UploadedFile``
|
|
|
|
object will have an additional ``image`` attribute containing the Pillow
|
|
|
|
`Image`_ instance used to check if the file was a valid image. Also,
|
|
|
|
``UploadedFile.content_type`` will be updated with the image's content type
|
|
|
|
if Pillow can determine it, otherwise it will be set to ``None``.
|
|
|
|
|
2016-04-28 22:09:57 +08:00
|
|
|
.. _Pillow: https://pillow.readthedocs.io/en/latest/
|
|
|
|
.. _Image: https://pillow.readthedocs.io/en/latest/reference/Image.html
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``IntegerField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: IntegerField(**kwargs)
|
|
|
|
|
2013-02-23 16:45:56 +08:00
|
|
|
* Default widget: :class:`NumberInput` when :attr:`Field.localize` is
|
|
|
|
``False``, else :class:`TextInput`.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
2017-01-20 02:19:26 +08:00
|
|
|
* Normalizes to: A Python integer.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that the given value is an integer. Leading and trailing
|
|
|
|
whitespace is allowed, as in Python's ``int()`` function.
|
|
|
|
* Error message keys: ``required``, ``invalid``, ``max_value``,
|
|
|
|
``min_value``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``max_value`` and ``min_value`` error messages may contain
|
|
|
|
``%(limit_value)s``, which will be substituted by the appropriate limit.
|
2010-12-29 05:10:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes two optional arguments for validation:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: max_value
|
|
|
|
.. attribute:: min_value
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
These control the range of values permitted in the field.
|
|
|
|
|
2011-06-11 21:48:24 +08:00
|
|
|
``GenericIPAddressField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------------
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. class:: GenericIPAddressField(**kwargs)
|
|
|
|
|
|
|
|
A field containing either an IPv4 or an IPv6 address.
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2011-06-11 21:48:24 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string. IPv6 addresses are normalized as described below.
|
2011-06-11 21:48:24 +08:00
|
|
|
* Validates that the given value is a valid IP address.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The IPv6 address normalization follows :rfc:`4291#section-2.2` section 2.2,
|
|
|
|
including using the IPv4 format suggested in paragraph 3 of that section, like
|
|
|
|
``::ffff:192.0.2.0``. For example, ``2001:0::0:01`` would be normalized to
|
|
|
|
``2001::1``, and ``::ffff:0a0a:0a0a`` to ``::ffff:10.10.10.10``. All characters
|
|
|
|
are converted to lowercase.
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes two optional arguments:
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: protocol
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Limits valid inputs to the specified protocol.
|
|
|
|
Accepted values are ``both`` (default), ``IPv4``
|
|
|
|
or ``IPv6``. Matching is case insensitive.
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: unpack_ipv4
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2012-11-08 00:13:06 +08:00
|
|
|
Unpacks IPv4 mapped addresses like ``::ffff:192.0.2.1``.
|
2011-10-14 08:12:01 +08:00
|
|
|
If this option is enabled that address would be unpacked to
|
|
|
|
``192.0.2.1``. Default is disabled. Can only be used
|
|
|
|
when ``protocol`` is set to ``'both'``.
|
2011-06-11 21:48:24 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``MultipleChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-----------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: MultipleChoiceField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`SelectMultiple`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``[]`` (an empty list)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A list of strings.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that every value in the given list of values exists in the list
|
|
|
|
of choices.
|
|
|
|
* Error message keys: ``required``, ``invalid_choice``, ``invalid_list``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``invalid_choice`` error message may contain ``%(value)s``, which will be
|
|
|
|
replaced with the selected choice.
|
2010-12-29 05:10:19 +08:00
|
|
|
|
2015-09-24 03:59:54 +08:00
|
|
|
Takes one extra required argument, ``choices``, as for :class:`ChoiceField`.
|
2010-12-05 12:47:19 +08:00
|
|
|
|
|
|
|
``TypedMultipleChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------------
|
2010-12-05 12:47:19 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. class:: TypedMultipleChoiceField(**kwargs)
|
|
|
|
|
|
|
|
Just like a :class:`MultipleChoiceField`, except :class:`TypedMultipleChoiceField`
|
|
|
|
takes two extra arguments, ``coerce`` and ``empty_value``.
|
2010-12-05 12:47:19 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`SelectMultiple`
|
2010-12-05 12:47:19 +08:00
|
|
|
* Empty value: Whatever you've given as ``empty_value``
|
|
|
|
* Normalizes to: A list of values of the type provided by the ``coerce``
|
|
|
|
argument.
|
|
|
|
* Validates that the given values exists in the list of choices and can be
|
|
|
|
coerced.
|
|
|
|
* Error message keys: ``required``, ``invalid_choice``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The ``invalid_choice`` error message may contain ``%(value)s``, which will be
|
|
|
|
replaced with the selected choice.
|
2010-12-29 05:10:19 +08:00
|
|
|
|
2015-09-24 03:59:54 +08:00
|
|
|
Takes two extra arguments, ``coerce`` and ``empty_value``, as for
|
|
|
|
:class:`TypedChoiceField`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``NullBooleanField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: NullBooleanField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`NullBooleanSelect`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``True``, ``False`` or ``None`` value.
|
|
|
|
* Validates nothing (i.e., it never raises a ``ValidationError``).
|
|
|
|
|
|
|
|
``RegexField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: RegexField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that the given value matches against a certain regular
|
|
|
|
expression.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one required argument:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: regex
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A regular expression specified either as a string or a compiled regular
|
|
|
|
expression object.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2015-02-05 20:26:15 +08:00
|
|
|
Also takes ``max_length``, ``min_length``, and ``strip``, which work just
|
2015-09-24 03:59:54 +08:00
|
|
|
as they do for :class:`CharField`.
|
2015-02-05 20:26:15 +08:00
|
|
|
|
|
|
|
.. attribute:: strip
|
|
|
|
|
|
|
|
Defaults to ``False``. If enabled, stripping will be applied before the
|
|
|
|
regex validation.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-03-03 17:01:13 +08:00
|
|
|
``SlugField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2010-03-03 17:01:13 +08:00
|
|
|
|
|
|
|
.. class:: SlugField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2010-03-03 17:01:13 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2010-11-09 13:18:47 +08:00
|
|
|
* Validates that the given value contains only letters, numbers,
|
|
|
|
underscores, and hyphens.
|
2010-03-03 17:01:13 +08:00
|
|
|
* Error messages: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
This field is intended for use in representing a model
|
|
|
|
:class:`~django.db.models.SlugField` in forms.
|
2010-03-03 17:01:13 +08:00
|
|
|
|
2015-04-16 06:28:49 +08:00
|
|
|
Takes an optional parameter:
|
|
|
|
|
|
|
|
.. attribute:: allow_unicode
|
|
|
|
|
|
|
|
A boolean instructing the field to accept Unicode letters in addition
|
|
|
|
to ASCII letters. Defaults to ``False``.
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``TimeField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: TimeField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``datetime.time`` object.
|
|
|
|
* Validates that the given value is either a ``datetime.time`` or string
|
|
|
|
formatted in a particular time format.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one optional argument:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: input_formats
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A list of formats used to attempt to convert a string to a valid
|
|
|
|
``datetime.time`` object.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If no ``input_formats`` argument is provided, the default input formats are::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
'%H:%M:%S', # '14:30:59'
|
|
|
|
'%H:%M', # '14:30'
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``URLField``
|
2016-01-03 18:56:22 +08:00
|
|
|
------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: URLField(**kwargs)
|
|
|
|
|
2013-01-28 21:24:48 +08:00
|
|
|
* Default widget: :class:`URLInput`
|
2008-08-24 06:25:40 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2008-08-24 06:25:40 +08:00
|
|
|
* Validates that the given value is a valid URL.
|
2012-03-31 21:55:03 +08:00
|
|
|
* Error message keys: ``required``, ``invalid``
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes the following optional arguments:
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: max_length
|
|
|
|
.. attribute:: min_length
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
These are the same as ``CharField.max_length`` and ``CharField.min_length``.
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2014-07-15 17:35:29 +08:00
|
|
|
``UUIDField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------
|
2014-07-15 17:35:29 +08:00
|
|
|
|
|
|
|
.. class:: UUIDField(**kwargs)
|
|
|
|
|
|
|
|
* Default widget: :class:`TextInput`
|
|
|
|
* Empty value: ``''`` (an empty string)
|
|
|
|
* Normalizes to: A :class:`~python:uuid.UUID` object.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
|
|
|
This field will accept any string format accepted as the ``hex`` argument
|
|
|
|
to the :class:`~python:uuid.UUID` constructor.
|
2010-02-22 07:44:35 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Slightly complex built-in ``Field`` classes
|
2016-01-03 18:56:22 +08:00
|
|
|
===========================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-03-17 04:59:15 +08:00
|
|
|
``ComboField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: ComboField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2010-03-17 04:59:15 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
2017-01-21 05:04:05 +08:00
|
|
|
* Normalizes to: A string.
|
2015-05-25 22:13:01 +08:00
|
|
|
* Validates the given value against each of the fields specified
|
2010-03-17 04:59:15 +08:00
|
|
|
as an argument to the ``ComboField``.
|
|
|
|
* Error message keys: ``required``, ``invalid``
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one extra required argument:
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: fields
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
The list of fields that should be used to validate the field's value (in
|
|
|
|
the order in which they are provided).
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django.forms import ComboField
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> f = ComboField(fields=[CharField(max_length=20), EmailField()])
|
|
|
|
>>> f.clean('test@example.com')
|
2014-03-23 04:30:49 +08:00
|
|
|
'test@example.com'
|
2011-10-14 08:12:01 +08:00
|
|
|
>>> f.clean('longemailaddress@example.com')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2014-03-23 04:30:49 +08:00
|
|
|
ValidationError: ['Ensure this value has at most 20 characters (it has 28).']
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2010-10-08 23:37:38 +08:00
|
|
|
``MultiValueField``
|
2016-01-03 18:56:22 +08:00
|
|
|
-------------------
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. class:: MultiValueField(fields=(), **kwargs)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`TextInput`
|
2010-03-17 04:59:15 +08:00
|
|
|
* Empty value: ``''`` (an empty string)
|
|
|
|
* Normalizes to: the type returned by the ``compress`` method of the subclass.
|
2015-05-25 22:13:01 +08:00
|
|
|
* Validates the given value against each of the fields specified
|
2010-03-17 04:59:15 +08:00
|
|
|
as an argument to the ``MultiValueField``.
|
2013-05-07 17:06:03 +08:00
|
|
|
* Error message keys: ``required``, ``invalid``, ``incomplete``
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
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 :class:`MultiValueField` must not
|
|
|
|
implement :meth:`~django.forms.Field.clean` but instead - implement
|
|
|
|
:meth:`~MultiValueField.compress`.
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes one extra required argument:
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: fields
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
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 by :meth:`~MultiValueField.compress`.
|
|
|
|
|
2017-05-06 06:24:27 +08:00
|
|
|
Also takes some optional arguments:
|
2013-05-07 17:06:03 +08:00
|
|
|
|
|
|
|
.. attribute:: require_all_fields
|
|
|
|
|
|
|
|
Defaults to ``True``, in which case a ``required`` validation error
|
|
|
|
will be raised if no value is supplied for any field.
|
|
|
|
|
|
|
|
When set to ``False``, the :attr:`Field.required` attribute can be set
|
|
|
|
to ``False`` for individual fields to make them optional. If no value
|
|
|
|
is supplied for a required field, an ``incomplete`` validation error
|
|
|
|
will be raised.
|
|
|
|
|
|
|
|
A default ``incomplete`` error message can be defined on the
|
|
|
|
:class:`MultiValueField` 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 = {
|
2014-08-04 19:29:59 +08:00
|
|
|
'incomplete': 'Enter a country calling code and a phone number.',
|
2013-05-07 17:06:03 +08:00
|
|
|
}
|
|
|
|
# Or define a different message for each field.
|
|
|
|
fields = (
|
2016-06-03 03:56:13 +08:00
|
|
|
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,
|
|
|
|
),
|
2013-05-07 17:06:03 +08:00
|
|
|
)
|
2017-01-22 14:57:14 +08:00
|
|
|
super().__init__(
|
2013-12-25 19:02:43 +08:00
|
|
|
error_messages=error_messages, fields=fields,
|
2016-06-03 03:56:13 +08:00
|
|
|
require_all_fields=False, *args, **kwargs
|
|
|
|
)
|
2013-05-07 17:06:03 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. attribute:: MultiValueField.widget
|
|
|
|
|
|
|
|
Must be a subclass of :class:`django.forms.MultiWidget`.
|
2012-12-25 22:56:22 +08:00
|
|
|
Default value is :class:`~django.forms.TextInput`, which
|
2012-09-12 18:59:19 +08:00
|
|
|
probably is not very useful in this case.
|
|
|
|
|
|
|
|
.. method:: compress(data_list)
|
|
|
|
|
|
|
|
Takes a list of valid values and returns a "compressed" version of
|
|
|
|
those values -- in a single value. For example,
|
|
|
|
:class:`SplitDateTimeField` is a subclass which combines a time field
|
|
|
|
and a date field into a ``datetime`` object.
|
|
|
|
|
|
|
|
This method must be implemented in the subclasses.
|
2010-03-17 04:59:15 +08:00
|
|
|
|
|
|
|
``SplitDateTimeField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------
|
2010-03-17 04:59:15 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. class:: SplitDateTimeField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`SplitDateTimeWidget`
|
2009-03-23 00:13:06 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A Python ``datetime.datetime`` object.
|
|
|
|
* Validates that the given value is a ``datetime.datetime`` or string
|
|
|
|
formatted in a particular datetime format.
|
2011-02-28 05:16:58 +08:00
|
|
|
* Error message keys: ``required``, ``invalid``, ``invalid_date``,
|
|
|
|
``invalid_time``
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Takes two optional arguments:
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: input_date_formats
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A list of formats used to attempt to convert a string to a valid
|
|
|
|
``datetime.date`` object.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If no ``input_date_formats`` argument is provided, the default input formats
|
2015-09-24 03:59:54 +08:00
|
|
|
for :class:`DateField` are used.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: input_time_formats
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
A list of formats used to attempt to convert a string to a valid
|
|
|
|
``datetime.time`` object.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
If no ``input_time_formats`` argument is provided, the default input formats
|
2015-09-24 03:59:54 +08:00
|
|
|
for :class:`TimeField` are used.
|
2009-03-23 00:13:06 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Fields which handle relationships
|
2016-01-03 18:56:22 +08:00
|
|
|
=================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-03-08 11:20:55 +08:00
|
|
|
Two fields are available for representing relationships between
|
|
|
|
models: :class:`ModelChoiceField` and
|
|
|
|
:class:`ModelMultipleChoiceField`. Both of these fields require a
|
|
|
|
single ``queryset`` parameter that is used to create the choices for
|
|
|
|
the field. Upon form validation, these fields will place either one
|
|
|
|
model object (in the case of ``ModelChoiceField``) or multiple model
|
|
|
|
objects (in the case of ``ModelMultipleChoiceField``) into the
|
|
|
|
``cleaned_data`` dictionary of the form.
|
|
|
|
|
2014-08-27 01:52:20 +08:00
|
|
|
For more complex uses, you can specify ``queryset=None`` when declaring the
|
|
|
|
form field and then populate the ``queryset`` in the form's ``__init__()``
|
|
|
|
method::
|
|
|
|
|
|
|
|
class FooMultipleChoiceForm(forms.Form):
|
|
|
|
foo_select = forms.ModelMultipleChoiceField(queryset=None)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-22 14:57:14 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2014-08-27 01:52:20 +08:00
|
|
|
self.fields['foo_select'].queryset = ...
|
|
|
|
|
2010-03-08 11:20:55 +08:00
|
|
|
``ModelChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
--------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. class:: ModelChoiceField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`Select`
|
2010-10-14 18:03:08 +08:00
|
|
|
* Empty value: ``None``
|
|
|
|
* Normalizes to: A model instance.
|
|
|
|
* Validates that the given id exists in the queryset.
|
|
|
|
* Error message keys: ``required``, ``invalid_choice``
|
|
|
|
|
2012-05-11 04:01:11 +08:00
|
|
|
Allows the selection of a single model object, suitable for representing a
|
|
|
|
foreign key. Note that the default widget for ``ModelChoiceField`` becomes
|
|
|
|
impractical when the number of entries increases. You should avoid using it
|
|
|
|
for more than 100 items.
|
|
|
|
|
|
|
|
A single argument is required:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: queryset
|
2008-08-27 13:57:27 +08:00
|
|
|
|
2017-01-27 04:03:58 +08:00
|
|
|
A ``QuerySet`` of model objects from which the choices for the field
|
|
|
|
are derived and which is used to validate the user's selection. It's
|
|
|
|
evaluated when the form is rendered.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2014-03-06 02:23:25 +08:00
|
|
|
``ModelChoiceField`` also takes two optional arguments:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: empty_label
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
By default the ``<select>`` widget used by ``ModelChoiceField`` will have an
|
|
|
|
empty choice at the top of the list. You can change the text of this
|
|
|
|
label (which is ``"---------"`` by default) with the ``empty_label``
|
|
|
|
attribute, or you can disable the empty label entirely by setting
|
|
|
|
``empty_label`` to ``None``::
|
2009-07-15 21:54:11 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
# A custom empty label
|
|
|
|
field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
|
2009-07-15 21:54:11 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
# No empty label
|
|
|
|
field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-04 02:30:54 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Note that if a ``ModelChoiceField`` is required and has a default
|
|
|
|
initial value, no empty choice is created (regardless of the value
|
|
|
|
of ``empty_label``).
|
2009-05-10 15:44:27 +08:00
|
|
|
|
2014-03-06 02:23:25 +08:00
|
|
|
.. attribute:: to_field_name
|
|
|
|
|
|
|
|
This optional argument is used to specify the field to use as the value
|
2014-08-27 19:43:13 +08:00
|
|
|
of the choices in the field's widget. Be sure it's a unique field for
|
|
|
|
the model, otherwise the selected value could match more than one
|
|
|
|
object. By default it is set to ``None``, in which case the primary key
|
|
|
|
of each object will be used. For example::
|
2014-03-06 02:23:25 +08:00
|
|
|
|
|
|
|
# No custom to_field_name
|
|
|
|
field1 = forms.ModelChoiceField(queryset=...)
|
|
|
|
|
|
|
|
would yield:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<select id="id_field1" name="field1">
|
|
|
|
<option value="obj1.pk">Object1</option>
|
|
|
|
<option value="obj2.pk">Object2</option>
|
|
|
|
...
|
|
|
|
</select>
|
|
|
|
|
|
|
|
and::
|
|
|
|
|
|
|
|
# to_field_name provided
|
|
|
|
field2 = forms.ModelChoiceField(queryset=..., to_field_name="name")
|
|
|
|
|
|
|
|
would yield:
|
|
|
|
|
|
|
|
.. code-block:: html
|
|
|
|
|
|
|
|
<select id="id_field2" name="field2">
|
|
|
|
<option value="obj1.name">Object1</option>
|
|
|
|
<option value="obj2.name">Object2</option>
|
|
|
|
...
|
|
|
|
</select>
|
|
|
|
|
2017-01-19 00:51:29 +08:00
|
|
|
The ``__str__()`` method of the model will be called to generate string
|
|
|
|
representations of the objects for use in the field's choices. To provide
|
|
|
|
customized representations, subclass ``ModelChoiceField`` and override
|
|
|
|
``label_from_instance``. This method will receive a model object and should
|
|
|
|
return a string suitable for representing it. For example::
|
2010-03-08 11:20:55 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
from django.forms import ModelChoiceField
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class MyModelChoiceField(ModelChoiceField):
|
|
|
|
def label_from_instance(self, obj):
|
|
|
|
return "My Object #%i" % obj.id
|
2010-03-08 11:20:55 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
``ModelMultipleChoiceField``
|
2016-01-03 18:56:22 +08:00
|
|
|
----------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-03-08 11:20:55 +08:00
|
|
|
.. class:: ModelMultipleChoiceField(**kwargs)
|
|
|
|
|
2012-12-29 18:30:12 +08:00
|
|
|
* Default widget: :class:`SelectMultiple`
|
2012-10-04 00:50:12 +08:00
|
|
|
* Empty value: An empty ``QuerySet`` (self.queryset.none())
|
|
|
|
* Normalizes to: A ``QuerySet`` of model instances.
|
2010-10-14 18:03:08 +08:00
|
|
|
* Validates that every id in the given list of values exists in the
|
|
|
|
queryset.
|
2010-11-09 13:18:47 +08:00
|
|
|
* Error message keys: ``required``, ``list``, ``invalid_choice``,
|
2010-10-14 18:03:08 +08:00
|
|
|
``invalid_pk_value``
|
|
|
|
|
2014-03-24 23:42:56 +08:00
|
|
|
The ``invalid_choice`` message may contain ``%(value)s`` and the
|
|
|
|
``invalid_pk_value`` message may contain ``%(pk)s``, which will be
|
|
|
|
substituted by the appropriate values.
|
2013-04-19 02:38:07 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Allows the selection of one or more model objects, suitable for
|
|
|
|
representing a many-to-many relation. As with :class:`ModelChoiceField`,
|
|
|
|
you can use ``label_from_instance`` to customize the object
|
2016-07-07 00:11:43 +08:00
|
|
|
representations.
|
|
|
|
|
|
|
|
A single argument is required:
|
2010-03-08 11:20:55 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. attribute:: queryset
|
2010-03-08 11:20:55 +08:00
|
|
|
|
2016-07-07 00:11:43 +08:00
|
|
|
Same as :class:`ModelChoiceField.queryset`.
|
|
|
|
|
|
|
|
Takes one optional argument:
|
|
|
|
|
|
|
|
.. attribute:: to_field_name
|
|
|
|
|
|
|
|
Same as :class:`ModelChoiceField.to_field_name`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Creating custom fields
|
2016-01-03 18:56:22 +08:00
|
|
|
======================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
If the built-in ``Field`` classes don't meet your needs, you can easily create
|
|
|
|
custom ``Field`` classes. To do this, just 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``).
|
2015-08-10 16:55:49 +08:00
|
|
|
|
|
|
|
You can also customize how a field will be accessed by overriding
|
|
|
|
:meth:`~django.forms.Field.get_bound_field()`.
|