所有这些字段和部件都可以从 django.contrib.postgres.forms 模块中获得。
SimpleArrayField¶SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)¶一个映射到数组的字段。它由一个 HTML <input> 表示。
base_field¶这是一个必要的参数。
It specifies the underlying form field for the array. This is not used to render any HTML, but it is used to process the submitted data and validate it. For example:
>>> from django import forms
>>> from django.contrib.postgres.forms import SimpleArrayField
>>> class NumberListForm(forms.Form):
... numbers = SimpleArrayField(forms.IntegerField())
...
>>> form = NumberListForm({"numbers": "1,2,3"})
>>> form.is_valid()
True
>>> form.cleaned_data
{'numbers': [1, 2, 3]}
>>> form = NumberListForm({"numbers": "1,2,a"})
>>> form.is_valid()
False
delimiter¶This is an optional argument which defaults to a comma: ,. This
value is used to split the submitted data. It allows you to chain
SimpleArrayField for multidimensional data:
>>> from django import forms
>>> from django.contrib.postgres.forms import SimpleArrayField
>>> class GridForm(forms.Form):
... places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter="|")
...
>>> form = GridForm({"places": "1,2|2,1|4,3"})
>>> form.is_valid()
True
>>> form.cleaned_data
{'places': [[1, 2], [2, 1], [4, 3]]}
备注
该字段不支持定界符的转义,所以当定界符是底层字段中的有效字符时,要小心。定界符不需要只用一个字符。
max_length¶这是一个可选的参数,用于验证数组的长度是否超过了规定的长度。
min_length¶这是一个可选的参数,用于验证数组是否至少达到了指定的长度。
用户友好的表单
SimpleArrayField 在大多数情况下并不是特别方便用户使用,但是它是一种有用的方式来格式化来自客户端部件的数据以提交给服务器。
SplitArrayField¶SplitArrayField(base_field, size, remove_trailing_nulls=False)¶这个字段通过重现底层字段固定的次数来处理数组。
base_field¶这是一个必要的参数。它指定了要重复的表单字段。
size¶这是基础字段的固定使用次数。
remove_trailing_nulls¶默认情况下,这被设置为 False。当 False 时,重复字段的每个值都会被存储。当设置为 True 时,任何尾部为空白的值将从结果中删除。如果底层字段有 required=True,但 remove_trailing_nulls 是 True,那么只有在最后才允许有空值,并且空值会被消除。
一些例子:
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
["1", "2", "3"] # -> [1, 2, 3]
["1", "2", ""] # -> ValidationError - third entry required.
["1", "", "3"] # -> ValidationError - second entry required.
["", "2", ""] # -> ValidationError - first and third entries required.
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
["1", "2", "3"] # -> [1, 2, 3]
["1", "2", ""] # -> [1, 2, None]
["1", "", "3"] # -> [1, None, 3]
["", "2", ""] # -> [None, 2, None]
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
["1", "2", "3"] # -> [1, 2, 3]
["1", "2", ""] # -> [1, 2]
["1", "", "3"] # -> ValidationError - second entry required.
["", "2", ""] # -> ValidationError - first entry required.
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
["1", "2", "3"] # -> [1, 2, 3]
["1", "2", ""] # -> [1, 2]
["1", "", "3"] # -> [1, None, 3]
["", "2", ""] # -> [None, 2]
HStoreField¶HStoreField¶一个为 HStoreField 接受 JSON 编码数据的字段。它将所有的值(除了空值)转换为字符串。它由一个 HTML <textarea> 表示。
用户友好的表单
HStoreField 在大多数情况下对用户不是特别友好,但是它是一种有用的方式来格式化来自客户端部件的数据以提交给服务器。
备注
在某些情况下,可能需要要求或限制对某个字段有效的键。这可以使用 KeysValidator 来完成。
这组字段在接受范围数据方面都有类似的功能。它们基于 MultiValueField。它们将一个省略的值视为一个无边界的范围。它们还验证了下限不大于上限。所有这些字段都使用 RangeWidget。
IntegerRangeField¶IntegerRangeField¶Based on IntegerField and translates its input into
django.db.backends.postgresql.psycopg_any.NumericRange. Default for
IntegerRangeField and
BigIntegerRangeField.
DecimalRangeField¶DecimalRangeField¶Based on DecimalField and translates its input into
django.db.backends.postgresql.psycopg_any.NumericRange. Default for
DecimalRangeField.
DateTimeRangeField¶DateTimeRangeField¶Based on DateTimeField and translates its input into
django.db.backends.postgresql.psycopg_any.DateTimeTZRange. Default for
DateTimeRangeField.
DateRangeField¶DateRangeField¶Based on DateField and translates its input into
django.db.backends.postgresql.psycopg_any.DateRange. Default for
DateRangeField.
RangeWidget¶RangeWidget(base_widget, attrs=None)¶所有范围字段都使用的部件。基于 MultiWidget。
RangeWidget 有一个必要的参数:
base_widget¶一个 RangeWidget 由 base_widget 的二元元组组成。
decompress(value)¶取一个字段的单个“压缩”值,例如一个 DateRangeField,并返回一个元组,代表一个下界和上界。
5月 31, 2023