forms.txt 7.5 KB

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