summaryrefslogtreecommitdiff
path: root/docs/ref/contrib/postgres/forms.txt
blob: 3012b54a109a58efe513dc47528df9af01fee246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
===========================================
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

Fields
======

``SimpleArrayField``
--------------------

.. class:: SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)

    A field which maps to an array. It is represented by an HTML ``<input>``.

    .. 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:

        .. code-block:: pycon

            >>> from django import forms
            >>> from django.contrib.postgres.forms import SimpleArrayField

            >>> 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:

        .. code-block:: pycon

            >>> from django import forms
            >>> from django.contrib.postgres.forms import SimpleArrayField

            >>> 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.

``SplitArrayField``
-------------------

.. 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]

``HStoreField``
---------------

.. class:: HStoreField

    A field which accepts JSON encoded data for an
    :class:`~django.contrib.postgres.fields.HStoreField`. It casts all values
    (except nulls) to strings. It is represented by an HTML ``<textarea>``.

    .. 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::

        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`.

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
not greater than the upper bound. All of these fields use
:class:`~django.contrib.postgres.forms.RangeWidget`.

``IntegerRangeField``
~~~~~~~~~~~~~~~~~~~~~

.. class:: IntegerRangeField

    Based on :class:`~django.forms.IntegerField` and translates its input into
    ``django.db.backends.postgresql.psycopg_any.NumericRange``. Default for
    :class:`~django.contrib.postgres.fields.IntegerRangeField` and
    :class:`~django.contrib.postgres.fields.BigIntegerRangeField`.

``DecimalRangeField``
~~~~~~~~~~~~~~~~~~~~~

.. class:: DecimalRangeField

    Based on :class:`~django.forms.DecimalField` and translates its input into
    ``django.db.backends.postgresql.psycopg_any.NumericRange``. Default for
    :class:`~django.contrib.postgres.fields.DecimalRangeField`.

``DateTimeRangeField``
~~~~~~~~~~~~~~~~~~~~~~

.. class:: DateTimeRangeField

    Based on :class:`~django.forms.DateTimeField` and translates its input into
    ``django.db.backends.postgresql.psycopg_any.DateTimeTZRange``. Default for
    :class:`~django.contrib.postgres.fields.DateTimeRangeField`.

``DateRangeField``
~~~~~~~~~~~~~~~~~~

.. class:: DateRangeField

    Based on :class:`~django.forms.DateField` and translates its input into
    ``django.db.backends.postgresql.psycopg_any.DateRange``. Default for
    :class:`~django.contrib.postgres.fields.DateRangeField`.

Widgets
=======

``RangeWidget``
---------------

.. 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`,
        and returns a tuple representing a lower and upper bound.