“Django/docs/3.0.x/ref/contrib/postgres/forms”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/ref/contrib/postgres/forms
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:PostgreSQL 特定的表单域和小部件 — Django 文档}}
 
<div id="postgresql-specific-form-fields-and-widgets" class="section">
 
<div id="postgresql-specific-form-fields-and-widgets" class="section">
  
= PostgreSQL specific form fields and widgets =
+
= PostgreSQL 特定的表单字段和小部件 =
  
All of these fields and widgets are available from the
+
所有这些字段和小部件都可以从 <code>django.contrib.postgres.forms</code> 模块获得。
<code>django.contrib.postgres.forms</code> module.
 
  
 
<div id="fields" class="section">
 
<div id="fields" class="section">
第12行: 第12行:
 
<div id="simplearrayfield" class="section">
 
<div id="simplearrayfield" class="section">
  
=== <code>SimpleArrayField</code> ===
+
=== SimpleArrayField ===
  
 
<dl>
 
<dl>
<dt>''class'' <code>SimpleArrayField</code><span class="sig-paren">(</span>''<span class="n">base_field</span>'', ''<span class="n">delimiter</span><span class="o">=</span><span class="default_value">','</span>'', ''<span class="n">max_length</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="n">min_length</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">SimpleArrayField</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">base_field</span></span>'', ''<span class="n"><span class="pre">delimiter</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">','</span></span>'', ''<span class="n"><span class="pre">max_length</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="n"><span class="pre">min_length</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>A field which maps to an array. It is represented by an HTML <code>&lt;input&gt;</code>.</p>
+
<dd><p>映射到数组的字段。 它由 HTML <code>&lt;input&gt;</code> 表示。</p>
 
<dl>
 
<dl>
<dt><code>base_field</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">base_field</span></span></dt>
<dd><p>This is a required argument.</p>
+
<dd><p>这是一个必需的参数。</p>
<p>It specifies the underlying form field for the array. This is not used
+
<p>它指定数组的底层表单字段。 这不用于呈现任何 HTML,但用于处理提交的数据并对其进行验证。 例如:</p>
to render any HTML, but it is used to process the submitted data and
 
validate it. For example:</p>
 
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from django import forms
+
<syntaxhighlight lang="python">>>> from django import forms
&gt;&gt;&gt; from django.contrib.postgres.forms import SimpleArrayField
+
>>> from django.contrib.postgres.forms import SimpleArrayField
  
&gt;&gt;&gt; class NumberListForm(forms.Form):
+
>>> class NumberListForm(forms.Form):
 
...    numbers = SimpleArrayField(forms.IntegerField())
 
...    numbers = SimpleArrayField(forms.IntegerField())
  
&gt;&gt;&gt; form = NumberListForm({'numbers': '1,2,3'})
+
>>> form = NumberListForm({'numbers': '1,2,3'})
&gt;&gt;&gt; form.is_valid()
+
>>> form.is_valid()
 
True
 
True
&gt;&gt;&gt; form.cleaned_data
+
>>> form.cleaned_data
 
{'numbers': [1, 2, 3]}
 
{'numbers': [1, 2, 3]}
  
&gt;&gt;&gt; form = NumberListForm({'numbers': '1,2,a'})
+
>>> form = NumberListForm({'numbers': '1,2,a'})
&gt;&gt;&gt; form.is_valid()
+
>>> form.is_valid()
False</pre>
+
False</syntaxhighlight>
  
 
</div>
 
</div>
第48行: 第46行:
  
 
<dl>
 
<dl>
<dt><code>delimiter</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">delimiter</span></span></dt>
<dd><p>This is an optional argument which defaults to a comma: <code>,</code>. This
+
<dd><p>这是一个可选参数,默认为逗号:<code>,</code>。 该值用于拆分提交的数据。 它允许您为多维数据链接 <code>SimpleArrayField</code></p>
value is used to split the submitted data. It allows you to chain
 
<code>SimpleArrayField</code> for multidimensional data:</p>
 
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; from django import forms
+
<syntaxhighlight lang="python">>>> from django import forms
&gt;&gt;&gt; from django.contrib.postgres.forms import SimpleArrayField
+
>>> from django.contrib.postgres.forms import SimpleArrayField
  
&gt;&gt;&gt; class GridForm(forms.Form):
+
>>> class GridForm(forms.Form):
 
...    places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter='|')
 
...    places = SimpleArrayField(SimpleArrayField(IntegerField()), delimiter='|')
  
&gt;&gt;&gt; form = GridForm({'places': '1,2|2,1|4,3'})
+
>>> form = GridForm({'places': '1,2|2,1|4,3'})
&gt;&gt;&gt; form.is_valid()
+
>>> form.is_valid()
 
True
 
True
&gt;&gt;&gt; form.cleaned_data
+
>>> form.cleaned_data
{'places': [[1, 2], [2, 1], [4, 3]]}</pre>
+
{'places': [[1, 2], [2, 1], [4, 3]]}</syntaxhighlight>
  
 
</div>
 
</div>
第73行: 第69行:
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>The field does not support escaping of the delimiter, so be careful
+
<p>该字段不支持对分隔符进行转义,因此在分隔符是基础字段中的有效字符的情况下要小心。 分隔符不必只有一个字符。</p>
in cases where the delimiter is a valid character in the underlying
 
field. The delimiter does not need to be only one character.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>max_length</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">max_length</span></span></dt>
<dd><p>This is an optional argument which validates that the array does not
+
<dd><p>这是一个可选参数,用于验证数组不超过规定的长度。</p></dd></dl>
exceed the stated length.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>min_length</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">min_length</span></span></dt>
<dd><p>This is an optional argument which validates that the array reaches at
+
<dd><p>这是一个可选参数,用于验证数组是否至少达到了规定的长度。</p></dd></dl>
least the stated length.</p></dd></dl>
 
  
 
<div class="admonition-user-friendly-forms admonition">
 
<div class="admonition-user-friendly-forms admonition">
  
<p>User friendly forms</p>
+
<p>用户友好的表格</p>
<p><code>SimpleArrayField</code> is not particularly user friendly in most cases,
+
<p><code>SimpleArrayField</code> 在大多数情况下不是特别用户友好,但它是一种有用的方式来格式化来自客户端小部件的数据以提交到服务器。</p>
however it is a useful way to format data from a client-side widget for
 
submission to the server.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
第103行: 第93行:
 
<div id="splitarrayfield" class="section">
 
<div id="splitarrayfield" class="section">
  
=== <code>SplitArrayField</code> ===
+
=== SplitArrayField ===
  
 
<dl>
 
<dl>
<dt>''class'' <code>SplitArrayField</code><span class="sig-paren">(</span>''<span class="n">base_field</span>'', ''<span class="n">size</span>'', ''<span class="n">remove_trailing_nulls</span><span class="o">=</span><span class="default_value">False</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">SplitArrayField</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">base_field</span></span>'', ''<span class="n"><span class="pre">size</span></span>'', ''<span class="n"><span class="pre">remove_trailing_nulls</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This field handles arrays by reproducing the underlying field a fixed
+
<dd><p>该字段通过将基础字段复制固定次数来处理数组。</p>
number of times.</p>
 
 
<dl>
 
<dl>
<dt><code>base_field</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">base_field</span></span></dt>
<dd><p>This is a required argument. It specifies the form field to be
+
<dd><p>这是一个必需的参数。 它指定要重复的表单字段。</p></dd></dl>
repeated.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>size</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">size</span></span></dt>
<dd><p>This is the fixed number of times the underlying field will be used.</p></dd></dl>
+
<dd><p>这是基础字段将被使用的固定次数。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>remove_trailing_nulls</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">remove_trailing_nulls</span></span></dt>
<dd><p>By default, this is set to <code>False</code>. When <code>False</code>, each value from
+
<dd><p>默认情况下,它设置为 <code>False</code>。 当 <code>False</code> 时,存储重复字段中的每个值。 当设置为 <code>True</code> 时,任何空白的尾随值都将从结果中删除。 如果底层字段有 <code>required=True</code>,但 <code>remove_trailing_nulls</code> <code>True</code>,那么最后只允许空值,将被剥离。</p>
the repeated fields is stored. When set to <code>True</code>, any trailing
+
<p>一些例子:</p>
values which are blank will be stripped from the result. If the
 
underlying field has <code>required=True</code>, but <code>remove_trailing_nulls</code>
 
is <code>True</code>, then null values are only allowed at the end, and will be
 
stripped.</p>
 
<p>Some examples:</p>
 
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
+
<syntaxhighlight lang="python">SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=False)
  
['1', '2', '3']  # -&gt; [1, 2, 3]
+
['1', '2', '3']  # -> [1, 2, 3]
['1', '2', '']  # -&gt; ValidationError - third entry required.
+
['1', '2', '']  # -> ValidationError - third entry required.
['1', '', '3']  # -&gt; ValidationError - second entry required.
+
['1', '', '3']  # -> ValidationError - second entry required.
['', '2', '']  # -&gt; ValidationError - first and third entries required.
+
['', '2', '']  # -> ValidationError - first and third entries required.
  
 
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
 
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=False)
  
['1', '2', '3']  # -&gt; [1, 2, 3]
+
['1', '2', '3']  # -> [1, 2, 3]
['1', '2', '']  # -&gt; [1, 2, None]
+
['1', '2', '']  # -> [1, 2, None]
['1', '', '3']  # -&gt; [1, None, 3]
+
['1', '', '3']  # -> [1, None, 3]
['', '2', '']  # -&gt; [None, 2, None]
+
['', '2', '']  # -> [None, 2, None]
  
 
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
 
SplitArrayField(IntegerField(required=True), size=3, remove_trailing_nulls=True)
  
['1', '2', '3']  # -&gt; [1, 2, 3]
+
['1', '2', '3']  # -> [1, 2, 3]
['1', '2', '']  # -&gt; [1, 2]
+
['1', '2', '']  # -> [1, 2]
['1', '', '3']  # -&gt; ValidationError - second entry required.
+
['1', '', '3']  # -> ValidationError - second entry required.
['', '2', '']  # -&gt; ValidationError - first entry required.
+
['', '2', '']  # -> ValidationError - first entry required.
  
 
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
 
SplitArrayField(IntegerField(required=False), size=3, remove_trailing_nulls=True)
  
['1', '2', '3']  # -&gt; [1, 2, 3]
+
['1', '2', '3']  # -> [1, 2, 3]
['1', '2', '']  # -&gt; [1, 2]
+
['1', '2', '']  # -> [1, 2]
['1', '', '3']  # -&gt; [1, None, 3]
+
['1', '', '3']  # -> [1, None, 3]
['', '2', '']  # -&gt; [None, 2]</pre>
+
['', '2', '']  # -> [None, 2]</syntaxhighlight>
  
 
</div>
 
</div>
第168行: 第151行:
 
<div id="hstorefield" class="section">
 
<div id="hstorefield" class="section">
  
=== <code>HStoreField</code> ===
+
=== HStoreField ===
  
 
<dl>
 
<dl>
<dt>''class'' <code>HStoreField</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">HStoreField</span></span></dt>
<dd><p>A field which accepts JSON encoded data for an
+
<dd><p>接受 [[../fields#django.contrib.postgres.fields|HStoreField]] 的 JSON 编码数据的字段。 它将所有值(空值除外)转换为字符串。 它由 HTML <code>&lt;textarea&gt;</code> 表示。</p>
[[../fields#django.contrib.postgres.fields|<code>HStoreField</code>]]. It casts all values
 
(except nulls) to strings. It is represented by an HTML <code>&lt;textarea&gt;</code>.</p>
 
 
<div class="admonition-user-friendly-forms admonition">
 
<div class="admonition-user-friendly-forms admonition">
  
<p>User friendly forms</p>
+
<p>用户友好的表格</p>
<p><code>HStoreField</code> is not particularly user friendly in most cases,
+
<p><code>HStoreField</code> 在大多数情况下不是特别用户友好,但它是一种有用的方式来格式化来自客户端小部件的数据以提交到服务器。</p>
however it is a useful way to format data from a client-side widget for
 
submission to the server.</p>
 
  
 
</div>
 
</div>
 
<div class="admonition note">
 
<div class="admonition note">
  
<p>注解</p>
+
<p>笔记</p>
<p>On occasions it may be useful to require or restrict the keys which are
+
<p>有时,要求或限制对给定字段有效的键可能很有用。 这可以使用 [[../validators#django.contrib.postgres.validators|KeysValidator]] 来完成。</p>
valid for a given field. This can be done using the
 
[[../validators#django.contrib.postgres.validators|<code>KeysValidator</code>]].</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
第196行: 第173行:
 
<div id="jsonfield" class="section">
 
<div id="jsonfield" class="section">
  
=== <code>JSONField</code> ===
+
=== JSONField ===
  
 
<dl>
 
<dl>
<dt>''class'' <code>JSONField</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">JSONField</span></span></dt>
<dd><p>A field which accepts JSON encoded data for a
+
<dd><p>接受 [[../fields#django.contrib.postgres.fields|JSONField]] 的 JSON 编码数据的字段。 它由 HTML <code>&lt;textarea&gt;</code> 表示。</p>
[[../fields#django.contrib.postgres.fields|<code>JSONField</code>]]. It is represented by an
 
HTML <code>&lt;textarea&gt;</code>.</p>
 
 
<div class="admonition-user-friendly-forms admonition">
 
<div class="admonition-user-friendly-forms admonition">
  
<p>User friendly forms</p>
+
<p>用户友好的表格</p>
<p><code>JSONField</code> is not particularly user friendly in most cases, however
+
<p><code>JSONField</code> 在大多数情况下不是特别用户友好,但它是一种有用的方式来格式化来自客户端小部件的数据以提交到服务器。</p>
it is a useful way to format data from a client-side widget for
 
submission to the server.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
第216行: 第189行:
 
<div id="range-fields" class="section">
 
<div id="range-fields" class="section">
  
=== Range Fields ===
+
=== 范围字段 ===
  
This group of fields all share similar functionality for accepting range data.
+
这组字段都共享用于接受范围数据的相似功能。 它们基于 [[../../../forms/fields#django.forms|MultiValueField]]。 他们将一个省略的值视为一个无界范围。 他们还验证下限不大于上限。 所有这些字段都使用 [[#django.contrib.postgres.forms.RangeWidget|RangeWidget]]
They are based on [[../../../forms/fields#django.forms|<code>MultiValueField</code>]]. They treat one
 
omitted value as an unbounded range. They also validate that the lower bound is
 
not greater than the upper bound. All of these fields use
 
[[#django.contrib.postgres.forms.RangeWidget|<code>RangeWidget</code>]].
 
  
 
<div id="integerrangefield" class="section">
 
<div id="integerrangefield" class="section">
  
==== <code>IntegerRangeField</code> ====
+
==== IntegerRangeField ====
  
; ''class'' <code>IntegerRangeField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">IntegerRangeField</span></span>
: Based on [[../../../forms/fields#django.forms|<code>IntegerField</code>]] and translates its input into <code>NumericRange</code>. Default for [[../fields#django.contrib.postgres.fields|<code>IntegerRangeField</code>]] and [[../fields#django.contrib.postgres.fields|<code>BigIntegerRangeField</code>]].
+
: 基于 [[../../../forms/fields#django.forms|IntegerField]] 并将其输入转换为 <code>NumericRange</code>[[../fields#django.contrib.postgres.fields|IntegerRangeField]] [[../fields#django.contrib.postgres.fields|BigIntegerRangeField]] 的默认值。
  
  
第235行: 第204行:
 
<div id="decimalrangefield" class="section">
 
<div id="decimalrangefield" class="section">
  
==== <code>DecimalRangeField</code> ====
+
==== DecimalRangeField ====
  
 
<dl>
 
<dl>
<dt>''class'' <code>DecimalRangeField</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">DecimalRangeField</span></span></dt>
 
<dd><div class="versionadded">
 
<dd><div class="versionadded">
  
 
+
<p><span class="versionmodified added">2.2 版中的新功能。</span></p>
  
 
</div>
 
</div>
<p>Based on [[../../../forms/fields#django.forms|<code>DecimalField</code>]] and translates its input into
+
<p>基于 [[../../../forms/fields#django.forms|DecimalField]] 并将其输入转换为 <code>NumericRange</code>[[../fields#django.contrib.postgres.fields|DecimalRangeField]] 的默认值。</p></dd></dl>
<code>NumericRange</code>. Default for
 
[[../fields#django.contrib.postgres.fields|<code>DecimalRangeField</code>]].</p></dd></dl>
 
  
  
第252行: 第219行:
 
<div id="floatrangefield" class="section">
 
<div id="floatrangefield" class="section">
  
==== <code>FloatRangeField</code> ====
+
==== FloatRangeField ====
  
 
<dl>
 
<dl>
<dt>''class'' <code>FloatRangeField</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">FloatRangeField</span></span></dt>
<dd><p>Based on [[../../../forms/fields#django.forms|<code>FloatField</code>]] and translates its input into
+
<dd><p>基于 [[../../../forms/fields#django.forms|FloatField]] 并将其输入转换为 <code>NumericRange</code>[[../fields#django.contrib.postgres.fields|FloatRangeField]] 的默认值。</p>
<code>NumericRange</code>. Default for
 
[[../fields#django.contrib.postgres.fields|<code>FloatRangeField</code>]].</p>
 
 
<div class="deprecated">
 
<div class="deprecated">
  
<p><span class="versionmodified deprecated">2.2 版后已移除: </span>Use [[#django.contrib.postgres.forms.DecimalRangeField|<code>DecimalRangeField</code>]] instead.</p>
+
<p><span class="versionmodified deprecated"> 2.2 版起已弃用:</span> 改用 [[#django.contrib.postgres.forms.DecimalRangeField|DecimalRangeField]]</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第269行: 第234行:
 
<div id="datetimerangefield" class="section">
 
<div id="datetimerangefield" class="section">
  
==== <code>DateTimeRangeField</code> ====
+
==== DateTimeRangeField ====
  
; ''class'' <code>DateTimeRangeField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">DateTimeRangeField</span></span>
: Based on [[../../../forms/fields#django.forms|<code>DateTimeField</code>]] and translates its input into <code>DateTimeTZRange</code>. Default for [[../fields#django.contrib.postgres.fields|<code>DateTimeRangeField</code>]].
+
: 基于 [[../../../forms/fields#django.forms|DateTimeField]] 并将其输入转换为 <code>DateTimeTZRange</code>[[../fields#django.contrib.postgres.fields|DateTimeRangeField]] 的默认值。
  
  
第278行: 第243行:
 
<div id="daterangefield" class="section">
 
<div id="daterangefield" class="section">
  
==== <code>DateRangeField</code> ====
+
==== DateRangeField ====
  
; ''class'' <code>DateRangeField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">DateRangeField</span></span>
: Based on [[../../../forms/fields#django.forms|<code>DateField</code>]] and translates its input into <code>DateRange</code>. Default for [[../fields#django.contrib.postgres.fields|<code>DateRangeField</code>]].
+
: 基于 [[../../../forms/fields#django.forms|DateField]] 并将其输入转换为 <code>DateRange</code>[[../fields#django.contrib.postgres.fields|DateRangeField]] 的默认值。
  
  
第291行: 第256行:
 
<div id="widgets" class="section">
 
<div id="widgets" class="section">
  
== 小部件 ==
+
== 小工具 ==
  
 
<div id="rangewidget" class="section">
 
<div id="rangewidget" class="section">
  
=== <code>RangeWidget</code> ===
+
=== RangeWidget ===
  
 
<dl>
 
<dl>
<dt>''class'' <code>RangeWidget</code><span class="sig-paren">(</span>''<span class="n">base_widget</span>'', ''<span class="n">attrs</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">RangeWidget</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">base_widget</span></span>'', ''<span class="n"><span class="pre">attrs</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Widget used by all of the range fields.
+
<dd><p>所有范围字段使用的小部件。 基于 [[../../../forms/widgets#django.forms|MultiWidget]]</p>
Based on [[../../../forms/widgets#django.forms|<code>MultiWidget</code>]].</p>
+
<p>[[#django.contrib.postgres.forms.RangeWidget|RangeWidget]] 有一个必需的参数:</p>
<p>[[#django.contrib.postgres.forms.RangeWidget|<code>RangeWidget</code>]] has one required argument:</p>
 
 
<dl>
 
<dl>
<dt><code>base_widget</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">base_widget</span></span></dt>
<dd><p>A [[#django.contrib.postgres.forms.RangeWidget|<code>RangeWidget</code>]] comprises a 2-tuple of <code>base_widget</code>.</p></dd></dl>
+
<dd><p>[[#django.contrib.postgres.forms.RangeWidget|RangeWidget]] 包含 <code>base_widget</code> 的 2 元组。</p></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>decompress</code><span class="sig-paren">(</span>''<span class="n">value</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">decompress</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">value</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Takes a single &quot;compressed&quot; value of a field, for example a
+
<dd><p>获取字段的单个“压缩”值,例如 [[../fields#django.contrib.postgres.fields|DateRangeField]],并返回表示下限和上限的元组。</p></dd></dl>
[[../fields#django.contrib.postgres.fields|<code>DateRangeField</code>]],
 
and returns a tuple representing a lower and upper bound.</p></dd></dl>
 
 
</dd></dl>
 
</dd></dl>
  
第317行: 第279行:
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Django 3.0.x 中文文档]]
+
[[Category:Django 3.0.x 文档]]

2021年10月31日 (日) 04:09的最新版本

PostgreSQL 特定的表单字段和小部件

所有这些字段和小部件都可以从 django.contrib.postgres.forms 模块获得。

字段

SimpleArrayField

class SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)

映射到数组的字段。 它由 HTML <input> 表示。

base_field

这是一个必需的参数。

它指定数组的底层表单字段。 这不用于呈现任何 HTML,但用于处理提交的数据并对其进行验证。 例如:

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

这是一个可选参数,默认为逗号:,。 该值用于拆分提交的数据。 它允许您为多维数据链接 SimpleArrayField

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

class SplitArrayField(base_field, size, remove_trailing_nulls=False)

该字段通过将基础字段复制固定次数来处理数组。

base_field

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

size

这是基础字段将被使用的固定次数。

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]


HStoreField

class HStoreField

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

用户友好的表格

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

笔记

有时,要求或限制对给定字段有效的键可能很有用。 这可以使用 KeysValidator 来完成。


JSONField

class JSONField

接受 JSONField 的 JSON 编码数据的字段。 它由 HTML <textarea> 表示。

用户友好的表格

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


范围字段

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

IntegerRangeField

class IntegerRangeField
基于 IntegerField 并将其输入转换为 NumericRangeIntegerRangeFieldBigIntegerRangeField 的默认值。


DecimalRangeField

class DecimalRangeField

2.2 版中的新功能。

基于 DecimalField 并将其输入转换为 NumericRangeDecimalRangeField 的默认值。


FloatRangeField

class FloatRangeField

基于 FloatField 并将其输入转换为 NumericRangeFloatRangeField 的默认值。

自 2.2 版起已弃用: 改用 DecimalRangeField


DateTimeRangeField

class DateTimeRangeField
基于 DateTimeField 并将其输入转换为 DateTimeTZRangeDateTimeRangeField 的默认值。


DateRangeField

class DateRangeField
基于 DateField 并将其输入转换为 DateRangeDateRangeField 的默认值。


小工具

RangeWidget

class RangeWidget(base_widget, attrs=None)

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

RangeWidget 有一个必需的参数:

base_widget

RangeWidget 包含 base_widget 的 2 元组。

decompress(value)

获取字段的单个“压缩”值,例如 DateRangeField,并返回表示下限和上限的元组。