forms.txt 7.7 KB

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