forms.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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]