添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
class 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()
>>> 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()
>>> form.cleaned_data
{'places': [[1, 2], [2, 1], [4, 3]]}

该字段不支持定界符的转义,所以当定界符是底层字段中的有效字符时,要小心。定界符不需要只用一个字符。

class SplitArrayField(base_field, size, remove_trailing_nulls=False)

这个字段通过重现底层字段固定的次数来处理数组。

base_field

这是一个必要的参数。它指定了要重复的表单字段。

remove_trailing_nulls

默认情况下,这被设置为 False。当 False 时,重复字段的每个值都会被存储。当设置为 True 时,任何尾部为空白的值将从结果中删除。如果底层字段有 required=True,但 remove_trailing_nullsTrue,那么只有在最后才允许有空值,并且空值会被消除。

一些例子:

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]
class HStoreField

一个为 HStoreField 接受 JSON 编码数据的字段。它将所有的值(除了空值)转换为字符串。它由一个 HTML <textarea> 表示。

用户友好的表单

HStoreField 在大多数情况下对用户不是特别友好,但是它是一种有用的方式来格式化来自客户端部件的数据以提交给服务器。

在某些情况下,可能需要要求或限制对某个字段有效的键。这可以使用 KeysValidator 来完成。

范围字段

这组字段在接受范围数据方面都有类似的功能。它们基于 MultiValueField。它们将一个省略的值视为一个无边界的范围。它们还验证了下限不大于上限。所有这些字段都使用 RangeWidget

IntegerRangeField

class IntegerRangeField

Based on IntegerField and translates its input into django.db.backends.postgresql.psycopg_any.NumericRange. Default for IntegerRangeField and BigIntegerRangeField.

class DecimalRangeField

Based on DecimalField and translates its input into django.db.backends.postgresql.psycopg_any.NumericRange. Default for DecimalRangeField.

class DateTimeRangeField

Based on DateTimeField and translates its input into django.db.backends.postgresql.psycopg_any.DateTimeTZRange. Default for DateTimeRangeField.

class DateRangeField

Based on DateField and translates its input into django.db.backends.postgresql.psycopg_any.DateRange. Default for DateRangeField.

class RangeWidget(base_widget, attrs=None)

所有范围字段都使用的部件。基于 MultiWidget

RangeWidget 有一个必要的参数:

base_widget

一个 RangeWidgetbase_widget 的二元元组组成。