2
0

forms.txt 7.7 KB

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