forms.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. ``JSONField``
  111. -------------
  112. .. class:: JSONField
  113. A field which accepts JSON encoded data for a
  114. :class:`~django.contrib.postgres.fields.JSONField`. It is represented by an
  115. HTML ``<textarea>``.
  116. .. admonition:: User friendly forms
  117. ``JSONField`` is not particularly user friendly in most cases, however
  118. it is a useful way to format data from a client-side widget for
  119. submission to the server.
  120. Range Fields
  121. ------------
  122. This group of fields all share similar functionality for accepting range data.
  123. They are based on :class:`~django.forms.MultiValueField`. They treat one
  124. omitted value as an unbounded range. They also validate that the lower bound is
  125. not greater than the upper bound. All of these fields use
  126. :class:`~django.contrib.postgres.forms.RangeWidget`.
  127. ``IntegerRangeField``
  128. ~~~~~~~~~~~~~~~~~~~~~
  129. .. class:: IntegerRangeField
  130. Based on :class:`~django.forms.IntegerField` and translates its input into
  131. :class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
  132. :class:`~django.contrib.postgres.fields.IntegerRangeField` and
  133. :class:`~django.contrib.postgres.fields.BigIntegerRangeField`.
  134. ``FloatRangeField``
  135. ~~~~~~~~~~~~~~~~~~~
  136. .. class:: FloatRangeField
  137. Based on :class:`~django.forms.FloatField` and translates its input into
  138. :class:`~psycopg2:psycopg2.extras.NumericRange`. Default for
  139. :class:`~django.contrib.postgres.fields.FloatRangeField`.
  140. ``DateTimeRangeField``
  141. ~~~~~~~~~~~~~~~~~~~~~~
  142. .. class:: DateTimeRangeField
  143. Based on :class:`~django.forms.DateTimeField` and translates its input into
  144. :class:`~psycopg2:psycopg2.extras.DateTimeTZRange`. Default for
  145. :class:`~django.contrib.postgres.fields.DateTimeRangeField`.
  146. ``DateRangeField``
  147. ~~~~~~~~~~~~~~~~~~
  148. .. class:: DateRangeField
  149. Based on :class:`~django.forms.DateField` and translates its input into
  150. :class:`~psycopg2:psycopg2.extras.DateRange`. Default for
  151. :class:`~django.contrib.postgres.fields.DateRangeField`.
  152. Widgets
  153. =======
  154. ``RangeWidget``
  155. ---------------
  156. .. class:: RangeWidget(base_widget, attrs=None)
  157. Widget used by all of the range fields.
  158. Based on :class:`~django.forms.MultiWidget`.
  159. :class:`~RangeWidget` has one required argument:
  160. .. attribute:: base_widget
  161. A :class:`~RangeWidget` comprises a 2-tuple of ``base_widget``.
  162. .. method:: decompress(value)
  163. Takes a single "compressed" value of a field, for example a
  164. :class:`~django.contrib.postgres.fields.DateRangeField`,
  165. and returns a tuple representing and lower and upper bound.