2016-01-03 18:56:22 +08:00
|
|
|
===========================================
|
2014-03-27 00:44:21 +08:00
|
|
|
PostgreSQL specific form fields and widgets
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
All of these fields and widgets are available from the
|
|
|
|
``django.contrib.postgres.forms`` module.
|
|
|
|
|
|
|
|
.. currentmodule:: django.contrib.postgres.forms
|
|
|
|
|
2016-01-03 18:56:22 +08:00
|
|
|
Fields
|
|
|
|
======
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``SimpleArrayField``
|
|
|
|
--------------------
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
.. class:: SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)
|
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
A field which maps to an array. It is represented by an HTML ``<input>``.
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
.. attribute:: base_field
|
|
|
|
|
|
|
|
This is a required argument.
|
|
|
|
|
|
|
|
It specifies the underlying form field for the array. This is not used
|
|
|
|
to render any HTML, but it is used to process the submitted data and
|
|
|
|
validate it. For example::
|
|
|
|
|
|
|
|
>>> from django import forms
|
2018-05-13 01:37:42 +08:00
|
|
|
>>> from django.contrib.postgres.forms import SimpleArrayField
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
>>> class NumberListForm(forms.Form):
|
|
|
|
... numbers = SimpleArrayField(forms.IntegerField())
|
|
|
|
|
|
|
|
>>> form = NumberListForm({'numbers': '1,2,3'})
|
|
|
|
>>> form.is_valid()
|
|
|
|
True
|
|
|
|
>>> form.cleaned_data
|
|
|
|
{'numbers': [1, 2, 3]}
|
|
|
|
|
|
|
|
>>> form = NumberListForm({'numbers': '1,2,a'})
|
|
|
|
>>> form.is_valid()
|
|
|
|
False
|
|
|
|
|
|
|
|
.. attribute:: delimiter
|
|
|
|
|
|
|
|
This is an optional argument which defaults to a comma: ``,``. This
|
|
|
|
value is used to split the submitted data. It allows you to chain
|
|
|
|
``SimpleArrayField`` for multidimensional data::
|
|
|
|
|
|
|
|
>>> from django import forms
|
2018-05-13 01:37:42 +08:00
|
|
|
>>> from django.contrib.postgres.forms import SimpleArrayField
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
>>> class GridForm(forms.Form):
|
|
|
|
... places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter='|')
|
|
|
|
|
|
|
|
>>> form = GridForm({'places': '1,2|2,1|4,3'})
|
|
|
|
>>> form.is_valid()
|
|
|
|
True
|
|
|
|
>>> form.cleaned_data
|
|
|
|
{'places': [[1, 2], [2, 1], [4, 3]]}
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The field does not support escaping of the delimiter, so be careful
|
|
|
|
in cases where the delimiter is a valid character in the underlying
|
|
|
|
field. The delimiter does not need to be only one character.
|
|
|
|
|
|
|
|
.. attribute:: max_length
|
|
|
|
|
|
|
|
This is an optional argument which validates that the array does not
|
|
|
|
exceed the stated length.
|
|
|
|
|
|
|
|
.. attribute:: min_length
|
|
|
|
|
|
|
|
This is an optional argument which validates that the array reaches at
|
|
|
|
least the stated length.
|
|
|
|
|
|
|
|
.. admonition:: User friendly forms
|
|
|
|
|
|
|
|
``SimpleArrayField`` is not particularly user friendly in most cases,
|
|
|
|
however it is a useful way to format data from a client-side widget for
|
|
|
|
submission to the server.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``SplitArrayField``
|
|
|
|
-------------------
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
.. class:: SplitArrayField(base_field, size, remove_trailing_nulls=False)
|
|
|
|
|
|
|
|
This field handles arrays by reproducing the underlying field a fixed
|
|
|
|
number of times.
|
|
|
|
|
|
|
|
.. attribute:: base_field
|
|
|
|
|
|
|
|
This is a required argument. It specifies the form field to be
|
|
|
|
repeated.
|
|
|
|
|
|
|
|
.. attribute:: size
|
|
|
|
|
|
|
|
This is the fixed number of times the underlying field will be used.
|
|
|
|
|
|
|
|
.. attribute:: remove_trailing_nulls
|
|
|
|
|
|
|
|
By default, this is set to ``False``. When ``False``, each value from
|
|
|
|
the repeated fields is stored. When set to ``True``, any trailing
|
|
|
|
values which are blank will be stripped from the result. If the
|
|
|
|
underlying field has ``required=True``, but ``remove_trailing_nulls``
|
|
|
|
is ``True``, then null values are only allowed at the end, and will be
|
|
|
|
stripped.
|
|
|
|
|
|
|
|
Some examples::
|
|
|
|
|
|
|
|
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
|
|
|
|
|
|
|
|
['1', '2', '3'] # -> [1, 2, 3]
|
|
|
|
['1', '2', ''] # -> ValidationError - third entry required.
|
|
|
|
['1', '', '3'] # -> ValidationError - second entry required.
|
|
|
|
['', '2', ''] # -> ValidationError - first and third entries required.
|
|
|
|
|
|
|
|
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
|
|
|
|
|
|
|
|
['1', '2', '3'] # -> [1, 2, 3]
|
|
|
|
['1', '2', ''] # -> [1, 2, None]
|
|
|
|
['1', '', '3'] # -> [1, None, 3]
|
|
|
|
['', '2', ''] # -> [None, 2, None]
|
|
|
|
|
|
|
|
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
|
|
|
|
|
|
|
|
['1', '2', '3'] # -> [1, 2, 3]
|
|
|
|
['1', '2', ''] # -> [1, 2]
|
|
|
|
['1', '', '3'] # -> ValidationError - second entry required.
|
|
|
|
['', '2', ''] # -> ValidationError - first entry required.
|
|
|
|
|
|
|
|
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
|
|
|
|
|
|
|
|
['1', '2', '3'] # -> [1, 2, 3]
|
|
|
|
['1', '2', ''] # -> [1, 2]
|
|
|
|
['1', '', '3'] # -> [1, None, 3]
|
|
|
|
['', '2', ''] # -> [None, 2]
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``HStoreField``
|
|
|
|
---------------
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
.. class:: HStoreField
|
|
|
|
|
|
|
|
A field which accepts JSON encoded data for an
|
2016-12-09 08:17:02 +08:00
|
|
|
:class:`~django.contrib.postgres.fields.HStoreField`. It casts all values
|
|
|
|
(except nulls) to strings. It is represented by an HTML ``<textarea>``.
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
.. admonition:: User friendly forms
|
|
|
|
|
|
|
|
``HStoreField`` is not particularly user friendly in most cases,
|
|
|
|
however it is a useful way to format data from a client-side widget for
|
|
|
|
submission to the server.
|
|
|
|
|
|
|
|
.. note::
|
2014-11-18 07:42:54 +08:00
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
On occasions it may be useful to require or restrict the keys which are
|
|
|
|
valid for a given field. This can be done using the
|
|
|
|
:class:`~django.contrib.postgres.validators.KeysValidator`.
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
Range Fields
|
|
|
|
------------
|
|
|
|
|
|
|
|
This group of fields all share similar functionality for accepting range data.
|
|
|
|
They are based on :class:`~django.forms.MultiValueField`. They treat one
|
|
|
|
omitted value as an unbounded range. They also validate that the lower bound is
|
2015-01-18 10:20:42 +08:00
|
|
|
not greater than the upper bound. All of these fields use
|
2015-01-24 04:05:58 +08:00
|
|
|
:class:`~django.contrib.postgres.forms.RangeWidget`.
|
2015-01-11 00:14:20 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``IntegerRangeField``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
.. class:: IntegerRangeField
|
|
|
|
|
|
|
|
Based on :class:`~django.forms.IntegerField` and translates its input into
|
|
|
|
:class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
|
|
|
|
:class:`~django.contrib.postgres.fields.IntegerRangeField` and
|
|
|
|
:class:`~django.contrib.postgres.fields.BigIntegerRangeField`.
|
|
|
|
|
2018-10-03 07:17:23 +08:00
|
|
|
``DecimalRangeField``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. class:: DecimalRangeField
|
|
|
|
|
|
|
|
Based on :class:`~django.forms.DecimalField` and translates its input into
|
|
|
|
:class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
|
|
|
|
:class:`~django.contrib.postgres.fields.DecimalRangeField`.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``DateTimeRangeField``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
.. class:: DateTimeRangeField
|
|
|
|
|
|
|
|
Based on :class:`~django.forms.DateTimeField` and translates its input into
|
|
|
|
:class:`~psycopg2:psycopg2.extras.DateTimeTZRange`. Default for
|
|
|
|
:class:`~django.contrib.postgres.fields.DateTimeRangeField`.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``DateRangeField``
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
.. class:: DateRangeField
|
|
|
|
|
|
|
|
Based on :class:`~django.forms.DateField` and translates its input into
|
|
|
|
:class:`~psycopg2:psycopg2.extras.DateRange`. Default for
|
|
|
|
:class:`~django.contrib.postgres.fields.DateRangeField`.
|
2015-01-18 10:20:42 +08:00
|
|
|
|
|
|
|
Widgets
|
2016-01-03 18:56:22 +08:00
|
|
|
=======
|
2015-01-18 10:20:42 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``RangeWidget``
|
|
|
|
---------------
|
2015-01-18 10:20:42 +08:00
|
|
|
|
|
|
|
.. class:: RangeWidget(base_widget, attrs=None)
|
|
|
|
|
|
|
|
Widget used by all of the range fields.
|
|
|
|
Based on :class:`~django.forms.MultiWidget`.
|
|
|
|
|
|
|
|
:class:`~RangeWidget` has one required argument:
|
|
|
|
|
|
|
|
.. attribute:: base_widget
|
|
|
|
|
|
|
|
A :class:`~RangeWidget` comprises a 2-tuple of ``base_widget``.
|
|
|
|
|
|
|
|
.. method:: decompress(value)
|
|
|
|
|
|
|
|
Takes a single "compressed" value of a field, for example a
|
|
|
|
:class:`~django.contrib.postgres.fields.DateRangeField`,
|
2020-07-15 15:02:51 +08:00
|
|
|
and returns a tuple representing a lower and upper bound.
|