forms.txt 8.4 KB

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