1. ===========================================
    
  2. PostgreSQL specific form fields and widgets
    
  3. ===========================================
    
  4. 
    
  5. All of these fields and widgets are available from the
    
  6. ``django.contrib.postgres.forms`` module.
    
  7. 
    
  8. .. currentmodule:: django.contrib.postgres.forms
    
  9. 
    
  10. Fields
    
  11. ======
    
  12. 
    
  13. ``SimpleArrayField``
    
  14. --------------------
    
  15. 
    
  16. .. class:: SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)
    
  17. 
    
  18.     A field which maps to an array. It is represented by an HTML ``<input>``.
    
  19. 
    
  20.     .. attribute:: base_field
    
  21. 
    
  22.         This is a required argument.
    
  23. 
    
  24.         It specifies the underlying form field for the array. This is not used
    
  25.         to render any HTML, but it is used to process the submitted data and
    
  26.         validate it. For example::
    
  27. 
    
  28.             >>> from django import forms
    
  29.             >>> from django.contrib.postgres.forms import SimpleArrayField
    
  30. 
    
  31.             >>> class NumberListForm(forms.Form):
    
  32.             ...     numbers = SimpleArrayField(forms.IntegerField())
    
  33. 
    
  34.             >>> form = NumberListForm({'numbers': '1,2,3'})
    
  35.             >>> form.is_valid()
    
  36.             True
    
  37.             >>> form.cleaned_data
    
  38.             {'numbers': [1, 2, 3]}
    
  39. 
    
  40.             >>> form = NumberListForm({'numbers': '1,2,a'})
    
  41.             >>> form.is_valid()
    
  42.             False
    
  43. 
    
  44.     .. attribute:: delimiter
    
  45. 
    
  46.         This is an optional argument which defaults to a comma: ``,``. This
    
  47.         value is used to split the submitted data. It allows you to chain
    
  48.         ``SimpleArrayField`` for multidimensional data::
    
  49. 
    
  50.             >>> from django import forms
    
  51.             >>> from django.contrib.postgres.forms import SimpleArrayField
    
  52. 
    
  53.             >>> class GridForm(forms.Form):
    
  54.             ...     places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter='|')
    
  55. 
    
  56.             >>> form = GridForm({'places': '1,2|2,1|4,3'})
    
  57.             >>> form.is_valid()
    
  58.             True
    
  59.             >>> form.cleaned_data
    
  60.             {'places': [[1, 2], [2, 1], [4, 3]]}
    
  61. 
    
  62.         .. note::
    
  63. 
    
  64.             The field does not support escaping of the delimiter, so be careful
    
  65.             in cases where the delimiter is a valid character in the underlying
    
  66.             field. The delimiter does not need to be only one character.
    
  67. 
    
  68.     .. attribute:: max_length
    
  69. 
    
  70.         This is an optional argument which validates that the array does not
    
  71.         exceed the stated length.
    
  72. 
    
  73.     .. attribute:: min_length
    
  74. 
    
  75.         This is an optional argument which validates that the array reaches at
    
  76.         least the stated length.
    
  77. 
    
  78.     .. admonition:: User friendly forms
    
  79. 
    
  80.         ``SimpleArrayField`` is not particularly user friendly in most cases,
    
  81.         however it is a useful way to format data from a client-side widget for
    
  82.         submission to the server.
    
  83. 
    
  84. ``SplitArrayField``
    
  85. -------------------
    
  86. 
    
  87. .. class:: SplitArrayField(base_field, size, remove_trailing_nulls=False)
    
  88. 
    
  89.     This field handles arrays by reproducing the underlying field a fixed
    
  90.     number of times.
    
  91. 
    
  92.     .. attribute:: base_field
    
  93. 
    
  94.         This is a required argument. It specifies the form field to be
    
  95.         repeated.
    
  96. 
    
  97.     .. attribute:: size
    
  98. 
    
  99.         This is the fixed number of times the underlying field will be used.
    
  100. 
    
  101.     .. attribute:: remove_trailing_nulls
    
  102. 
    
  103.         By default, this is set to ``False``. When ``False``, each value from
    
  104.         the repeated fields is stored. When set to ``True``, any trailing
    
  105.         values which are blank will be stripped from the result. If the
    
  106.         underlying field has ``required=True``, but ``remove_trailing_nulls``
    
  107.         is ``True``, then null values are only allowed at the end, and will be
    
  108.         stripped.
    
  109. 
    
  110.         Some examples::
    
  111. 
    
  112.             SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
    
  113. 
    
  114.             ['1', '2', '3']  # -> [1, 2, 3]
    
  115.             ['1', '2', '']  # -> ValidationError - third entry required.
    
  116.             ['1', '', '3']  # -> ValidationError - second entry required.
    
  117.             ['', '2', '']  # -> ValidationError - first and third entries required.
    
  118. 
    
  119.             SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
    
  120. 
    
  121.             ['1', '2', '3']  # -> [1, 2, 3]
    
  122.             ['1', '2', '']  # -> [1, 2, None]
    
  123.             ['1', '', '3']  # -> [1, None, 3]
    
  124.             ['', '2', '']  # -> [None, 2, None]
    
  125. 
    
  126.             SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
    
  127. 
    
  128.             ['1', '2', '3']  # -> [1, 2, 3]
    
  129.             ['1', '2', '']  # -> [1, 2]
    
  130.             ['1', '', '3']  # -> ValidationError - second entry required.
    
  131.             ['', '2', '']  # -> ValidationError - first entry required.
    
  132. 
    
  133.             SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
    
  134. 
    
  135.             ['1', '2', '3']  # -> [1, 2, 3]
    
  136.             ['1', '2', '']  # -> [1, 2]
    
  137.             ['1', '', '3']  # -> [1, None, 3]
    
  138.             ['', '2', '']  # -> [None, 2]
    
  139. 
    
  140. ``HStoreField``
    
  141. ---------------
    
  142. 
    
  143. .. class:: HStoreField
    
  144. 
    
  145.     A field which accepts JSON encoded data for an
    
  146.     :class:`~django.contrib.postgres.fields.HStoreField`. It casts all values
    
  147.     (except nulls) to strings. It is represented by an HTML ``<textarea>``.
    
  148. 
    
  149.     .. admonition:: User friendly forms
    
  150. 
    
  151.         ``HStoreField`` is not particularly user friendly in most cases,
    
  152.         however it is a useful way to format data from a client-side widget for
    
  153.         submission to the server.
    
  154. 
    
  155.     .. note::
    
  156. 
    
  157.         On occasions it may be useful to require or restrict the keys which are
    
  158.         valid for a given field. This can be done using the
    
  159.         :class:`~django.contrib.postgres.validators.KeysValidator`.
    
  160. 
    
  161. Range Fields
    
  162. ------------
    
  163. 
    
  164. This group of fields all share similar functionality for accepting range data.
    
  165. They are based on :class:`~django.forms.MultiValueField`. They treat one
    
  166. omitted value as an unbounded range. They also validate that the lower bound is
    
  167. not greater than the upper bound. All of these fields use
    
  168. :class:`~django.contrib.postgres.forms.RangeWidget`.
    
  169. 
    
  170. ``IntegerRangeField``
    
  171. ~~~~~~~~~~~~~~~~~~~~~
    
  172. 
    
  173. .. class:: IntegerRangeField
    
  174. 
    
  175.     Based on :class:`~django.forms.IntegerField` and translates its input into
    
  176.     :class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
    
  177.     :class:`~django.contrib.postgres.fields.IntegerRangeField` and
    
  178.     :class:`~django.contrib.postgres.fields.BigIntegerRangeField`.
    
  179. 
    
  180. ``DecimalRangeField``
    
  181. ~~~~~~~~~~~~~~~~~~~~~
    
  182. 
    
  183. .. class:: DecimalRangeField
    
  184. 
    
  185.     Based on :class:`~django.forms.DecimalField` and translates its input into
    
  186.     :class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
    
  187.     :class:`~django.contrib.postgres.fields.DecimalRangeField`.
    
  188. 
    
  189. ``DateTimeRangeField``
    
  190. ~~~~~~~~~~~~~~~~~~~~~~
    
  191. 
    
  192. .. class:: DateTimeRangeField
    
  193. 
    
  194.     Based on :class:`~django.forms.DateTimeField` and translates its input into
    
  195.     :class:`~psycopg2:psycopg2.extras.DateTimeTZRange`. Default for
    
  196.     :class:`~django.contrib.postgres.fields.DateTimeRangeField`.
    
  197. 
    
  198. ``DateRangeField``
    
  199. ~~~~~~~~~~~~~~~~~~
    
  200. 
    
  201. .. class:: DateRangeField
    
  202. 
    
  203.     Based on :class:`~django.forms.DateField` and translates its input into
    
  204.     :class:`~psycopg2:psycopg2.extras.DateRange`. Default for
    
  205.     :class:`~django.contrib.postgres.fields.DateRangeField`.
    
  206. 
    
  207. Widgets
    
  208. =======
    
  209. 
    
  210. ``RangeWidget``
    
  211. ---------------
    
  212. 
    
  213. .. class:: RangeWidget(base_widget, attrs=None)
    
  214. 
    
  215.     Widget used by all of the range fields.
    
  216.     Based on :class:`~django.forms.MultiWidget`.
    
  217. 
    
  218.     :class:`~RangeWidget` has one required argument:
    
  219. 
    
  220.     .. attribute:: base_widget
    
  221. 
    
  222.         A :class:`~RangeWidget` comprises a 2-tuple of ``base_widget``.
    
  223. 
    
  224.     .. method:: decompress(value)
    
  225. 
    
  226.         Takes a single "compressed" value of a field, for example a
    
  227.         :class:`~django.contrib.postgres.fields.DateRangeField`,
    
  228.         and returns a tuple representing a lower and upper bound.