forms.txt 8.1 KB

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