{"title":"模型字段参考","version":"2.2","locale":"zh-hans","docname":"ref/models/fields","url":"/zh-hans/2.2/ref/models/fields/","canonical":"https://djangodocs.dev/zh-hans/2.2/ref/models/fields/","summary":"本文档包含 Field 类的所有 API 参考，包括 field options 和 field types。以下参数对所以字段类型均有效，且是可选的。","html":"<section id=\"module-django.db.models.fields\">\n<span id=\"model-field-reference\"></span><h1>模型字段参考<a class=\"heading-anchor\" href=\"#module-django.db.models.fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>本文档包含 <a class=\"reference internal\" href=\"#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a> 类的所有 API 参考，包括 <a class=\"reference internal\" href=\"#field-options\">field options</a> 和 <a class=\"reference internal\" href=\"#field-types\">field types</a>。</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>若内置字段未满足需求，你可以试试 <a class=\"reference external\" href=\"https://github.com/django/django-localflavor\">django-localflavor</a> (<a class=\"reference external\" href=\"https://django-localflavor.readthedocs.io/\">文档</a>)，它包含了针对各别国家和文件的代码。</p>\n<p>当然，你也可以简单的 <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/\"><span class=\"doc\">编写自定义模型字段</span></a>。</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>技巧性的，这些方法都被定义在 <a class=\"reference internal\" href=\"#module-django.db.models.fields\" title=\"django.db.models.fields: Built-in field types.\"><code class=\"xref py py-mod docutils literal notranslate\">django.db.models.fields</code></a>，但出于方便的目的，它们被导入 <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#module-django.db.models\" title=\"django.db.models\"><code class=\"xref py py-mod docutils literal notranslate\">django.db.models</code></a>；标准的转换是使用 <code class=\"docutils literal notranslate\">from django.db import models</code> 并利用 <code class=\"docutils literal notranslate\">models.&lt;Foo&gt;Field</code> 字段。</p>\n</aside>\n<section id=\"field-options\">\n<span id=\"common-model-field-options\"></span><h2>字段选项<a class=\"heading-anchor\" href=\"#field-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>以下参数对所以字段类型均有效，且是可选的。</p>\n<section id=\"null\">\n<h3><code class=\"docutils literal notranslate\">null</code><a class=\"heading-anchor\" href=\"#null\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.null\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">null</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.null\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>如果设置为 <code class=\"docutils literal notranslate\">True</code>， 当该字段为空时，Django 会将数据库中该字段设置为 <code class=\"docutils literal notranslate\">NULL</code>，默认为 <code class=\"docutils literal notranslate\">False</code>。</p>\n<p>避免在基于字符串的字段（例如 <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> 和 <a class=\"reference internal\" href=\"#django.db.models.TextField\" title=\"django.db.models.TextField\"><code class=\"xref py py-class docutils literal notranslate\">TextField</code></a>）上使用 <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a>。如果字符串字段的 <code class=\"docutils literal notranslate\">null=True</code>，那意味着对于“无数据”有两个可能的值：<code class=\"docutils literal notranslate\">NULL</code> 和空字符串。在大多数情况下，对于“无数据”声明两个值是赘余的，Django 的惯例是使用空字符串而不是 <code class=\"docutils literal notranslate\">NULL</code>。 一个例外是当 <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> 同时具有 <code class=\"docutils literal notranslate\">unique=True</code> 和 <code class=\"docutils literal notranslate\">blank=True</code> 时。 在这种情况下，需要设置 <code class=\"docutils literal notranslate\">null=True</code>，以便在使用空白值保存多个对象时避免唯一的约束违规。</p>\n<p>For both string-based and non-string-based fields, you will also need to\nset <code class=\"docutils literal notranslate\">blank=True</code> if you wish to permit empty values in forms, as the\n<a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a> parameter only affects database storage\n(see <a class=\"reference internal\" href=\"#django.db.models.Field.blank\" title=\"django.db.models.Field.blank\"><code class=\"xref py py-attr docutils literal notranslate\">blank</code></a>).</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>When using the Oracle database backend, the value <code class=\"docutils literal notranslate\">NULL</code> will be stored to\ndenote the empty string regardless of this attribute.</p>\n</aside>\n</section>\n<section id=\"blank\">\n<h3><code class=\"docutils literal notranslate\">blank</code><a class=\"heading-anchor\" href=\"#blank\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.blank\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">blank</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.blank\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>如果设置为 <code class=\"docutils literal notranslate\">True</code> ，该字段允许为空。默认为 <code class=\"docutils literal notranslate\">False</code> 。</p>\n<p>Note that this is different than <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a>. <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a> is\npurely database-related, whereas <a class=\"reference internal\" href=\"#django.db.models.Field.blank\" title=\"django.db.models.Field.blank\"><code class=\"xref py py-attr docutils literal notranslate\">blank</code></a> is validation-related. If\na field has <code class=\"docutils literal notranslate\">blank=True</code>, form validation will allow entry of an empty value.\nIf a field has <code class=\"docutils literal notranslate\">blank=False</code>, the field will be required.</p>\n</section>\n<section id=\"choices\">\n<span id=\"field-choices\"></span><h3><code class=\"docutils literal notranslate\">choices</code><a class=\"heading-anchor\" href=\"#choices\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.choices\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">choices</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.choices\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A <a class=\"reference external\" href=\"https://docs.python.org/3/glossary.html#term-sequence\" title=\"(in Python v3.14)\"><span class=\"xref std std-term\">sequence</span></a> consisting itself of iterables of exactly two items (e.g.\n<code class=\"docutils literal notranslate\">[(A, B), (A, B) ...]</code>) to use as choices for this field. If choices are\ngiven, they're enforced by <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#validating-objects\"><span class=\"std std-ref\">model validation</span></a> and the\ndefault form widget will be a select box with these choices instead of the\nstandard text field.</p>\n<p>The first element in each tuple is the actual value to be set on the model,\nand the second element is the human-readable name. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>YEAR_IN_SCHOOL_CHOICES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;FR&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Freshman&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;SO&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Sophomore&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;JR&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Junior&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;SR&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Senior&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Generally, it's best to define choices inside a model class, and to\ndefine a suitably-named constant for each value:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Student</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    FRESHMAN <span class=\"o\">=</span> <span class=\"s1\">&#39;FR&#39;</span>\n    SOPHOMORE <span class=\"o\">=</span> <span class=\"s1\">&#39;SO&#39;</span>\n    JUNIOR <span class=\"o\">=</span> <span class=\"s1\">&#39;JR&#39;</span>\n    SENIOR <span class=\"o\">=</span> <span class=\"s1\">&#39;SR&#39;</span>\n    YEAR_IN_SCHOOL_CHOICES <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"p\">(</span>FRESHMAN<span class=\"p\">,</span> <span class=\"s1\">&#39;Freshman&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">(</span>SOPHOMORE<span class=\"p\">,</span> <span class=\"s1\">&#39;Sophomore&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">(</span>JUNIOR<span class=\"p\">,</span> <span class=\"s1\">&#39;Junior&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">(</span>SENIOR<span class=\"p\">,</span> <span class=\"s1\">&#39;Senior&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n    year_in_school <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>\n        max_length<span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">,</span>\n        choices<span class=\"o\">=</span>YEAR_IN_SCHOOL_CHOICES<span class=\"p\">,</span>\n        default<span class=\"o\">=</span>FRESHMAN<span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">is_upperclass</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>year_in_school <span class=\"ow\">in</span> <span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span>JUNIOR<span class=\"p\">,</span> <span class=\"bp\">self</span><span class=\"o\">.</span>SENIOR<span class=\"p\">)</span>\n</code></pre></div>\n<p>Though you can define a choices list outside of a model class and then\nrefer to it, defining the choices and names for each choice inside the\nmodel class keeps all of that information with the class that uses it,\nand makes the choices easy to reference (e.g, <code class=\"docutils literal notranslate\">Student.SOPHOMORE</code>\nwill work anywhere that the <code class=\"docutils literal notranslate\">Student</code> model has been imported).</p>\n<p>You can also collect your available choices into named groups that can\nbe used for organizational purposes:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>MEDIA_CHOICES <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;Audio&#39;</span><span class=\"p\">,</span> <span class=\"p\">(</span>\n            <span class=\"p\">(</span><span class=\"s1\">&#39;vinyl&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Vinyl&#39;</span><span class=\"p\">),</span>\n            <span class=\"p\">(</span><span class=\"s1\">&#39;cd&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;CD&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">)</span>\n    <span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;Video&#39;</span><span class=\"p\">,</span> <span class=\"p\">(</span>\n            <span class=\"p\">(</span><span class=\"s1\">&#39;vhs&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;VHS Tape&#39;</span><span class=\"p\">),</span>\n            <span class=\"p\">(</span><span class=\"s1\">&#39;dvd&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;DVD&#39;</span><span class=\"p\">),</span>\n        <span class=\"p\">)</span>\n    <span class=\"p\">),</span>\n    <span class=\"p\">(</span><span class=\"s1\">&#39;unknown&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Unknown&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>The first element in each tuple is the name to apply to the group. The\nsecond element is an iterable of 2-tuples, with each 2-tuple containing\na value and a human-readable name for an option. Grouped options may be\ncombined with ungrouped options within a single list (such as the\nunknown option in this example).</p>\n<p>For each model field that has <a class=\"reference internal\" href=\"#django.db.models.Field.choices\" title=\"django.db.models.Field.choices\"><code class=\"xref py py-attr docutils literal notranslate\">choices</code></a> set, Django will add a\nmethod to retrieve the human-readable name for the field's current value. See\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.get_FOO_display\" title=\"django.db.models.Model.get_FOO_display\"><code class=\"xref py py-meth docutils literal notranslate\">get_FOO_display()</code></a> in the database API\ndocumentation.</p>\n<p>Note that choices can be any sequence object -- not necessarily a list or\ntuple. This lets you construct choices dynamically. But if you find yourself\nhacking <a class=\"reference internal\" href=\"#django.db.models.Field.choices\" title=\"django.db.models.Field.choices\"><code class=\"xref py py-attr docutils literal notranslate\">choices</code></a> to be dynamic, you're probably better off using\na proper database table with a <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>. <a class=\"reference internal\" href=\"#django.db.models.Field.choices\" title=\"django.db.models.Field.choices\"><code class=\"xref py py-attr docutils literal notranslate\">choices</code></a> is\nmeant for static data that doesn't change much, if ever.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>每当 <code class=\"docutils literal notranslate\">choices</code> 的顺序变动时将会创建新的迁移。</p>\n</aside>\n<p>Unless <a class=\"reference internal\" href=\"#django.db.models.Field.blank\" title=\"django.db.models.Field.blank\"><code class=\"xref py py-attr docutils literal notranslate\">blank=False</code></a> is set on the field along with a\n<a class=\"reference internal\" href=\"#django.db.models.Field.default\" title=\"django.db.models.Field.default\"><code class=\"xref py py-attr docutils literal notranslate\">default</code></a> then a label containing <code class=\"docutils literal notranslate\">&quot;---------&quot;</code> will be rendered\nwith the select box. To override this behavior, add a tuple to <code class=\"docutils literal notranslate\">choices</code>\ncontaining <code class=\"docutils literal notranslate\">None</code>; e.g. <code class=\"docutils literal notranslate\">(None, 'Your String For Display')</code>.\nAlternatively, you can use an empty string instead of <code class=\"docutils literal notranslate\">None</code> where this makes\nsense - such as on a <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a>.</p>\n</section>\n<section id=\"db-column\">\n<h3><code class=\"docutils literal notranslate\">db_column</code><a class=\"heading-anchor\" href=\"#db-column\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.db_column\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">db_column</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.db_column\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the database column to use for this field. If this isn't given,\nDjango will use the field's name.</p>\n<p>If your database column name is an SQL reserved word, or contains\ncharacters that aren't allowed in Python variable names -- notably, the\nhyphen -- that's OK. Django quotes column and table names behind the\nscenes.</p>\n</section>\n<section id=\"db-index\">\n<h3><code class=\"docutils literal notranslate\">db_index</code><a class=\"heading-anchor\" href=\"#db-index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.db_index\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">db_index</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.db_index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If <code class=\"docutils literal notranslate\">True</code>, a database index will be created for this field.</p>\n</section>\n<section id=\"db-tablespace\">\n<h3><code class=\"docutils literal notranslate\">db_tablespace</code><a class=\"heading-anchor\" href=\"#db-tablespace\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.db_tablespace\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">db_tablespace</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.db_tablespace\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/tablespaces/\"><span class=\"doc\">database tablespace</span></a> to use for\nthis field's index, if this field is indexed. The default is the project's\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-DEFAULT_INDEX_TABLESPACE\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_INDEX_TABLESPACE</code></a> setting, if set, or the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/options/#django.db.models.Options.db_tablespace\" title=\"django.db.models.Options.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\">db_tablespace</code></a> of the model, if any. If the backend doesn't\nsupport tablespaces for indexes, this option is ignored.</p>\n</section>\n<section id=\"default\">\n<h3><code class=\"docutils literal notranslate\">default</code><a class=\"heading-anchor\" href=\"#default\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.default\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">default</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.default\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>该字段的默认值。可以是一个值或者是个可调用的对象，如果是个可调用对象，每次实例化模型时都会调用该对象。</p>\n<p>The default can't be a mutable object (model instance, <code class=\"docutils literal notranslate\">list</code>, <code class=\"docutils literal notranslate\">set</code>, etc.),\nas a reference to the same instance of that object would be used as the default\nvalue in all new model instances. Instead, wrap the desired default in a\ncallable. For example, if you want to specify a default <code class=\"docutils literal notranslate\">dict</code> for\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/postgres/fields/#django.contrib.postgres.fields.JSONField\" title=\"django.contrib.postgres.fields.JSONField\"><code class=\"xref py py-class docutils literal notranslate\">JSONField</code></a>, use a function:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">contact_default</span><span class=\"p\">():</span>\n    <span class=\"k\">return</span> <span class=\"p\">{</span><span class=\"s2\">&quot;email&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;to1@example.com&quot;</span><span class=\"p\">}</span>\n\ncontact_info <span class=\"o\">=</span> JSONField<span class=\"p\">(</span><span class=\"s2\">&quot;ContactInfo&quot;</span><span class=\"p\">,</span> default<span class=\"o\">=</span>contact_default<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">lambda</code>s can't be used for field options like <code class=\"docutils literal notranslate\">default</code> because they\ncan't be <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/migrations/#migration-serializing\"><span class=\"std std-ref\">serialized by migrations</span></a>. See that\ndocumentation for other caveats.</p>\n<p>For fields like <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> that map to model instances, defaults\nshould be the value of the field they reference (<code class=\"docutils literal notranslate\">pk</code> unless\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey.to_field\" title=\"django.db.models.ForeignKey.to_field\"><code class=\"xref py py-attr docutils literal notranslate\">to_field</code></a> is set) instead of model instances.</p>\n<p>The default value is used when new model instances are created and a value\nisn't provided for the field. When the field is a primary key, the default is\nalso used when the field is set to <code class=\"docutils literal notranslate\">None</code>.</p>\n</section>\n<section id=\"editable\">\n<h3><code class=\"docutils literal notranslate\">editable</code><a class=\"heading-anchor\" href=\"#editable\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.editable\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">editable</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.editable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>If <code class=\"docutils literal notranslate\">False</code>, the field will not be displayed in the admin or any other\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>. They are also skipped during <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#validating-objects\"><span class=\"std std-ref\">model\nvalidation</span></a>. Default is <code class=\"docutils literal notranslate\">True</code>.</p>\n</section>\n<section id=\"error-messages\">\n<h3><code class=\"docutils literal notranslate\">error_messages</code><a class=\"heading-anchor\" href=\"#error-messages\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.error_messages\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">error_messages</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.error_messages\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The <code class=\"docutils literal notranslate\">error_messages</code> argument lets you override the default messages that the\nfield will raise. Pass in a dictionary with keys matching the error messages you\nwant to override.</p>\n<p>Error message keys include <code class=\"docutils literal notranslate\">null</code>, <code class=\"docutils literal notranslate\">blank</code>, <code class=\"docutils literal notranslate\">invalid</code>, <code class=\"docutils literal notranslate\">invalid_choice</code>,\n<code class=\"docutils literal notranslate\">unique</code>, and <code class=\"docutils literal notranslate\">unique_for_date</code>. Additional error message keys are\nspecified for each field in the <a class=\"reference internal\" href=\"#field-types\">Field types</a> section below.</p>\n<p>These error messages often don't propagate to forms. See\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#considerations-regarding-model-errormessages\"><span class=\"std std-ref\">有关模型的 error_messages 的注意事项</span></a>.</p>\n</section>\n<section id=\"help-text\">\n<h3><code class=\"docutils literal notranslate\">help_text</code><a class=\"heading-anchor\" href=\"#help-text\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.help_text\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">help_text</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.help_text\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>额外的“帮助”文本，随表单控件一同显示。即便你的字段未用于表单，它对于生成文档也是很有用的。</p>\n<p>Note that this value is <em>not</em> HTML-escaped in automatically-generated\nforms. This lets you include HTML in <a class=\"reference internal\" href=\"#django.db.models.Field.help_text\" title=\"django.db.models.Field.help_text\"><code class=\"xref py py-attr docutils literal notranslate\">help_text</code></a> if you so\ndesire. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>help_text<span class=\"o\">=</span><span class=\"s2\">&quot;Please use the following format: &lt;em&gt;YYYY-MM-DD&lt;/em&gt;.&quot;</span>\n</code></pre></div>\n<p>Alternatively you can use plain text and\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/utils/#django.utils.html.escape\" title=\"django.utils.html.escape\"><code class=\"xref py py-func docutils literal notranslate\">django.utils.html.escape()</code></a> to escape any HTML special characters. Ensure\nthat you escape any help text that may come from untrusted users to avoid a\ncross-site scripting attack.</p>\n</section>\n<section id=\"primary-key\">\n<h3><code class=\"docutils literal notranslate\">primary_key</code><a class=\"heading-anchor\" href=\"#primary-key\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.primary_key\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">primary_key</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.primary_key\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>如果设置为 <code class=\"docutils literal notranslate\">True</code> ，将该字段设置为该模型的主键。</p>\n<p>If you don't specify <code class=\"docutils literal notranslate\">primary_key=True</code> for any field in your model, Django\nwill automatically add an <a class=\"reference internal\" href=\"#django.db.models.AutoField\" title=\"django.db.models.AutoField\"><code class=\"xref py py-class docutils literal notranslate\">AutoField</code></a> to hold the primary key, so you\ndon't need to set <code class=\"docutils literal notranslate\">primary_key=True</code> on any of your fields unless you want to\noverride the default primary-key behavior. For more, see\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#automatic-primary-key-fields\"><span class=\"std std-ref\">自动设置主键</span></a>.</p>\n<p><code class=\"docutils literal notranslate\">primary_key=True</code> implies <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null=False</code></a> and\n<a class=\"reference internal\" href=\"#django.db.models.Field.unique\" title=\"django.db.models.Field.unique\"><code class=\"xref py py-attr docutils literal notranslate\">unique=True</code></a>. Only one primary key is allowed on an\nobject.</p>\n<p>The primary key field is read-only. If you change the value of the primary\nkey on an existing object and then save it, a new object will be created\nalongside the old one.</p>\n</section>\n<section id=\"unique\">\n<h3><code class=\"docutils literal notranslate\">unique</code><a class=\"heading-anchor\" href=\"#unique\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.unique\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">unique</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.unique\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>如果设置为 <code class=\"docutils literal notranslate\">True</code>，这个字段必须在整个表中保持值唯一。</p>\n<p>This is enforced at the database level and by model validation. If\nyou try to save a model with a duplicate value in a <a class=\"reference internal\" href=\"#django.db.models.Field.unique\" title=\"django.db.models.Field.unique\"><code class=\"xref py py-attr docutils literal notranslate\">unique</code></a>\nfield, a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/exceptions/#django.db.IntegrityError\" title=\"django.db.IntegrityError\"><code class=\"xref py py-exc docutils literal notranslate\">django.db.IntegrityError</code></a> will be raised by the model's\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.save\" title=\"django.db.models.Model.save\"><code class=\"xref py py-meth docutils literal notranslate\">save()</code></a> method.</p>\n<p>This option is valid on all field types except <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> and\n<a class=\"reference internal\" href=\"#django.db.models.OneToOneField\" title=\"django.db.models.OneToOneField\"><code class=\"xref py py-class docutils literal notranslate\">OneToOneField</code></a>.</p>\n<p>Note that when <code class=\"docutils literal notranslate\">unique</code> is <code class=\"docutils literal notranslate\">True</code>, you don't need to specify\n<a class=\"reference internal\" href=\"#django.db.models.Field.db_index\" title=\"django.db.models.Field.db_index\"><code class=\"xref py py-attr docutils literal notranslate\">db_index</code></a>, because <code class=\"docutils literal notranslate\">unique</code> implies the creation of an index.</p>\n</section>\n<section id=\"unique-for-date\">\n<h3><code class=\"docutils literal notranslate\">unique_for_date</code><a class=\"heading-anchor\" href=\"#unique-for-date\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.unique_for_date\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">unique_for_date</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.unique_for_date\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Set this to the name of a <a class=\"reference internal\" href=\"#django.db.models.DateField\" title=\"django.db.models.DateField\"><code class=\"xref py py-class docutils literal notranslate\">DateField</code></a> or <a class=\"reference internal\" href=\"#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\">DateTimeField</code></a> to\nrequire that this field be unique for the value of the date field.</p>\n<p>For example, if you have a field <code class=\"docutils literal notranslate\">title</code> that has\n<code class=\"docutils literal notranslate\">unique_for_date=&quot;pub_date&quot;</code>, then Django wouldn't allow the entry of two\nrecords with the same <code class=\"docutils literal notranslate\">title</code> and <code class=\"docutils literal notranslate\">pub_date</code>.</p>\n<p>Note that if you set this to point to a <a class=\"reference internal\" href=\"#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\">DateTimeField</code></a>, only the date\nportion of the field will be considered. Besides, when <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\">USE_TZ</code></a> is\n<code class=\"docutils literal notranslate\">True</code>, the check will be performed in the <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/i18n/timezones/#default-current-time-zone\"><span class=\"std std-ref\">current time zone</span></a> at the time the object gets saved.</p>\n<p>This is enforced by <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.validate_unique\" title=\"django.db.models.Model.validate_unique\"><code class=\"xref py py-meth docutils literal notranslate\">Model.validate_unique()</code></a> during model validation\nbut not at the database level. If any <a class=\"reference internal\" href=\"#django.db.models.Field.unique_for_date\" title=\"django.db.models.Field.unique_for_date\"><code class=\"xref py py-attr docutils literal notranslate\">unique_for_date</code></a> constraint\ninvolves fields that are not part of a <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a> (for\nexample, if one of the fields is listed in <code class=\"docutils literal notranslate\">exclude</code> or has\n<a class=\"reference internal\" href=\"#django.db.models.Field.editable\" title=\"django.db.models.Field.editable\"><code class=\"xref py py-attr docutils literal notranslate\">editable=False</code></a>), <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.validate_unique\" title=\"django.db.models.Model.validate_unique\"><code class=\"xref py py-meth docutils literal notranslate\">Model.validate_unique()</code></a> will\nskip validation for that particular constraint.</p>\n</section>\n<section id=\"unique-for-month\">\n<h3><code class=\"docutils literal notranslate\">unique_for_month</code><a class=\"heading-anchor\" href=\"#unique-for-month\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.unique_for_month\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">unique_for_month</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.unique_for_month\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like <a class=\"reference internal\" href=\"#django.db.models.Field.unique_for_date\" title=\"django.db.models.Field.unique_for_date\"><code class=\"xref py py-attr docutils literal notranslate\">unique_for_date</code></a>, but requires the field to be unique with\nrespect to the month.</p>\n</section>\n<section id=\"unique-for-year\">\n<h3><code class=\"docutils literal notranslate\">unique_for_year</code><a class=\"heading-anchor\" href=\"#unique-for-year\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.unique_for_year\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">unique_for_year</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.unique_for_year\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like <a class=\"reference internal\" href=\"#django.db.models.Field.unique_for_date\" title=\"django.db.models.Field.unique_for_date\"><code class=\"xref py py-attr docutils literal notranslate\">unique_for_date</code></a> and <a class=\"reference internal\" href=\"#django.db.models.Field.unique_for_month\" title=\"django.db.models.Field.unique_for_month\"><code class=\"xref py py-attr docutils literal notranslate\">unique_for_month</code></a>.</p>\n</section>\n<section id=\"verbose-name\">\n<h3><code class=\"docutils literal notranslate\">verbose_name</code><a class=\"heading-anchor\" href=\"#verbose-name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.verbose_name\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">verbose_name</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.verbose_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A human-readable name for the field. If the verbose name isn't given, Django\nwill automatically create it using the field's attribute name, converting\nunderscores to spaces. See <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#verbose-field-names\"><span class=\"std std-ref\">Verbose field names</span></a>.</p>\n</section>\n<section id=\"validators\">\n<h3><code class=\"docutils literal notranslate\">validators</code><a class=\"heading-anchor\" href=\"#validators\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.validators\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">validators</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.validators\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A list of validators to run for this field. See the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/\"><span class=\"doc\">validators\ndocumentation</span></a> for more information.</p>\n<section id=\"registering-and-fetching-lookups\">\n<h4>Registering and fetching lookups<a class=\"heading-anchor\" href=\"#registering-and-fetching-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><code class=\"docutils literal notranslate\">Field</code> implements the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/lookups/#lookup-registration-api\"><span class=\"std std-ref\">lookup registration API</span></a>.\nThe API can be used to customize which lookups are available for a field class, and\nhow lookups are fetched from a field.</p>\n</section>\n</section>\n</section>\n<section id=\"field-types\">\n<span id=\"model-field-types\"></span><h2>字段类型<a class=\"heading-anchor\" href=\"#field-types\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"autofield\">\n<h3><code class=\"docutils literal notranslate\">AutoField</code><a class=\"heading-anchor\" href=\"#autofield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.AutoField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">AutoField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.AutoField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>An <a class=\"reference internal\" href=\"#django.db.models.IntegerField\" title=\"django.db.models.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\">IntegerField</code></a> that automatically increments\naccording to available IDs. You usually won't need to use this directly; a\nprimary key field will automatically be added to your model if you don't specify\notherwise. See <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#automatic-primary-key-fields\"><span class=\"std std-ref\">自动设置主键</span></a>.</p>\n</section>\n<section id=\"bigautofield\">\n<h3><code class=\"docutils literal notranslate\">BigAutoField</code><a class=\"heading-anchor\" href=\"#bigautofield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.BigAutoField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BigAutoField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.BigAutoField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A 64-bit integer, much like an <a class=\"reference internal\" href=\"#django.db.models.AutoField\" title=\"django.db.models.AutoField\"><code class=\"xref py py-class docutils literal notranslate\">AutoField</code></a> except that it is\nguaranteed to fit numbers from <code class=\"docutils literal notranslate\">1</code> to <code class=\"docutils literal notranslate\">9223372036854775807</code>.</p>\n</section>\n<section id=\"bigintegerfield\">\n<h3><code class=\"docutils literal notranslate\">BigIntegerField</code><a class=\"heading-anchor\" href=\"#bigintegerfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.BigIntegerField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BigIntegerField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.BigIntegerField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A 64-bit integer, much like an <a class=\"reference internal\" href=\"#django.db.models.IntegerField\" title=\"django.db.models.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\">IntegerField</code></a> except that it is\nguaranteed to fit numbers from <code class=\"docutils literal notranslate\">-9223372036854775808</code> to\n<code class=\"docutils literal notranslate\">9223372036854775807</code>. The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>.</p>\n</section>\n<section id=\"binaryfield\">\n<h3><code class=\"docutils literal notranslate\">BinaryField</code><a class=\"heading-anchor\" href=\"#binaryfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.BinaryField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BinaryField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.BinaryField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A field to store raw binary data. It can be assigned <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#bytes\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">bytes</code></a>,\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#bytearray\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">bytearray</code></a>, or <a class=\"reference external\" href=\"https://docs.python.org/3/library/stdtypes.html#memoryview\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">memoryview</code></a>.</p>\n<p>By default, <code class=\"docutils literal notranslate\">BinaryField</code> sets <a class=\"reference internal\" href=\"#django.db.models.Field.editable\" title=\"django.db.models.Field.editable\"><code class=\"xref py py-attr docutils literal notranslate\">editable</code></a> to <code class=\"docutils literal notranslate\">False</code>, in which\ncase it can't be included in a <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>.</p>\n<aside class=\"version-note version-changed\" data-version=\"2.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 2.1</p><p>Older versions don't allow setting <code class=\"docutils literal notranslate\">editable</code> to <code class=\"docutils literal notranslate\">True</code>.</p>\n</aside>\n<p><code class=\"docutils literal notranslate\">BinaryField</code> has one extra optional argument:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.BinaryField.max_length\">\n<span class=\"sig-prename descclassname\">BinaryField.</span><span class=\"sig-name descname\">max_length</span><a class=\"heading-anchor\" href=\"#django.db.models.BinaryField.max_length\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The maximum length (in characters) of the field. The maximum length is\nenforced in Django's validation using\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.MaxLengthValidator\" title=\"django.core.validators.MaxLengthValidator\"><code class=\"xref py py-class docutils literal notranslate\">MaxLengthValidator</code></a>.</p>\n</dd></dl>\n\n<aside class=\"admonition-abusing-binaryfield admonition\" role=\"note\">\n<p class=\"admonition-title\">Abusing <code class=\"docutils literal notranslate\">BinaryField</code></p>\n<p>Although you might think about storing files in the database, consider that\nit is bad design in 99% of the cases. This field is <em>not</em> a replacement for\nproper <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/static-files/\"><span class=\"doc\">static files</span></a> handling.</p>\n</aside>\n</section>\n<section id=\"booleanfield\">\n<h3><code class=\"docutils literal notranslate\">BooleanField</code><a class=\"heading-anchor\" href=\"#booleanfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.BooleanField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BooleanField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.BooleanField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A true/false field.</p>\n<p>The default form widget for this field is <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.CheckboxInput\" title=\"django.forms.CheckboxInput\"><code class=\"xref py py-class docutils literal notranslate\">CheckboxInput</code></a>,\nor <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.NullBooleanSelect\" title=\"django.forms.NullBooleanSelect\"><code class=\"xref py py-class docutils literal notranslate\">NullBooleanSelect</code></a> if <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null=True</code></a>.</p>\n<p>The default value of <code class=\"docutils literal notranslate\">BooleanField</code> is <code class=\"docutils literal notranslate\">None</code> when <a class=\"reference internal\" href=\"#django.db.models.Field.default\" title=\"django.db.models.Field.default\"><code class=\"xref py py-attr docutils literal notranslate\">Field.default</code></a>\nisn't defined.</p>\n<aside class=\"version-note version-changed\" data-version=\"2.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 2.1</p><p>In older versions, this field doesn't permit <code class=\"docutils literal notranslate\">null=True</code>, so you have to\nuse <a class=\"reference internal\" href=\"#django.db.models.NullBooleanField\" title=\"django.db.models.NullBooleanField\"><code class=\"xref py py-class docutils literal notranslate\">NullBooleanField</code></a> instead. Using the latter is now discouraged\nas it's likely to be deprecated in a future version of Django.</p>\n<p>In older versions, this field has <a class=\"reference internal\" href=\"#django.db.models.Field.blank\" title=\"django.db.models.Field.blank\"><code class=\"xref py py-attr docutils literal notranslate\">blank=True</code></a>\nimplicitly. You can restore the previous behavior by setting\n<code class=\"docutils literal notranslate\">blank=True</code>.</p>\n</aside>\n</section>\n<section id=\"charfield\">\n<h3><code class=\"docutils literal notranslate\">CharField</code><a class=\"heading-anchor\" href=\"#charfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.CharField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">CharField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.CharField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A string field, for small- to large-sized strings.</p>\n<p>For large amounts of text, use <a class=\"reference internal\" href=\"#django.db.models.TextField\" title=\"django.db.models.TextField\"><code class=\"xref py py-class docutils literal notranslate\">TextField</code></a>.</p>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> has one extra required argument:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.CharField.max_length\">\n<span class=\"sig-prename descclassname\">CharField.</span><span class=\"sig-name descname\">max_length</span><a class=\"heading-anchor\" href=\"#django.db.models.CharField.max_length\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The maximum length (in characters) of the field. The max_length is enforced\nat the database level and in Django's validation using\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.MaxLengthValidator\" title=\"django.core.validators.MaxLengthValidator\"><code class=\"xref py py-class docutils literal notranslate\">MaxLengthValidator</code></a>.</p>\n</dd></dl>\n\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you are writing an application that must be portable to multiple\ndatabase backends, you should be aware that there are restrictions on\n<code class=\"docutils literal notranslate\">max_length</code> for some backends. Refer to the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/databases/\"><span class=\"doc\">database backend\nnotes</span></a> for details.</p>\n</aside>\n</section>\n<section id=\"datefield\">\n<h3><code class=\"docutils literal notranslate\">DateField</code><a class=\"heading-anchor\" href=\"#datefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DateField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">DateField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">auto_now<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">auto_now_add<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.DateField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A date, represented in Python by a <code class=\"docutils literal notranslate\">datetime.date</code> instance. Has a few extra,\noptional arguments:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DateField.auto_now\">\n<span class=\"sig-prename descclassname\">DateField.</span><span class=\"sig-name descname\">auto_now</span><a class=\"heading-anchor\" href=\"#django.db.models.DateField.auto_now\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Automatically set the field to now every time the object is saved. Useful\nfor &quot;last-modified&quot; timestamps. Note that the current date is <em>always</em>\nused; it's not just a default value that you can override.</p>\n<p>The field is only automatically updated when calling <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.save\" title=\"django.db.models.Model.save\"><code class=\"xref py py-meth docutils literal notranslate\">Model.save()</code></a>. The field isn't updated when making updates\nto other fields in other ways such as <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#django.db.models.query.QuerySet.update\" title=\"django.db.models.query.QuerySet.update\"><code class=\"xref py py-meth docutils literal notranslate\">QuerySet.update()</code></a>, though you can specify a custom\nvalue for the field in an update like that.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DateField.auto_now_add\">\n<span class=\"sig-prename descclassname\">DateField.</span><span class=\"sig-name descname\">auto_now_add</span><a class=\"heading-anchor\" href=\"#django.db.models.DateField.auto_now_add\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Automatically set the field to now when the object is first created. Useful\nfor creation of timestamps. Note that the current date is <em>always</em> used;\nit's not just a default value that you can override. So even if you\nset a value for this field when creating the object, it will be ignored.\nIf you want to be able to modify this field, set the following instead of\n<code class=\"docutils literal notranslate\">auto_now_add=True</code>:</p>\n<ul class=\"simple\">\n<li><p>For <a class=\"reference internal\" href=\"#django.db.models.DateField\" title=\"django.db.models.DateField\"><code class=\"xref py py-class docutils literal notranslate\">DateField</code></a>: <code class=\"docutils literal notranslate\">default=date.today</code> - from\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.date.today\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">datetime.date.today()</code></a></p></li>\n<li><p>For <a class=\"reference internal\" href=\"#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\">DateTimeField</code></a>: <code class=\"docutils literal notranslate\">default=timezone.now</code> - from\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/utils/#django.utils.timezone.now\" title=\"django.utils.timezone.now\"><code class=\"xref py py-func docutils literal notranslate\">django.utils.timezone.now()</code></a></p></li>\n</ul>\n</dd></dl>\n\n<p>The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>. The admin adds a JavaScript calendar,\nand a shortcut for &quot;Today&quot;. Includes an additional <code class=\"docutils literal notranslate\">invalid_date</code> error\nmessage key.</p>\n<p>The options <code class=\"docutils literal notranslate\">auto_now_add</code>, <code class=\"docutils literal notranslate\">auto_now</code>, and <code class=\"docutils literal notranslate\">default</code> are mutually exclusive.\nAny combination of these options will result in an error.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>As currently implemented, setting <code class=\"docutils literal notranslate\">auto_now</code> or <code class=\"docutils literal notranslate\">auto_now_add</code> to\n<code class=\"docutils literal notranslate\">True</code> will cause the field to have <code class=\"docutils literal notranslate\">editable=False</code> and <code class=\"docutils literal notranslate\">blank=True</code>\nset.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The <code class=\"docutils literal notranslate\">auto_now</code> and <code class=\"docutils literal notranslate\">auto_now_add</code> options will always use the date in\nthe <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/i18n/timezones/#default-current-time-zone\"><span class=\"std std-ref\">default timezone</span></a> at the moment of\ncreation or update. If you need something different, you may want to\nconsider simply using your own callable default or overriding <code class=\"docutils literal notranslate\">save()</code>\ninstead of using <code class=\"docutils literal notranslate\">auto_now</code> or <code class=\"docutils literal notranslate\">auto_now_add</code>; or using a\n<code class=\"docutils literal notranslate\">DateTimeField</code> instead of a <code class=\"docutils literal notranslate\">DateField</code> and deciding how to handle the\nconversion from datetime to date at display time.</p>\n</aside>\n</section>\n<section id=\"datetimefield\">\n<h3><code class=\"docutils literal notranslate\">DateTimeField</code><a class=\"heading-anchor\" href=\"#datetimefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DateTimeField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">DateTimeField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">auto_now<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">auto_now_add<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.DateTimeField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A date and time, represented in Python by a <code class=\"docutils literal notranslate\">datetime.datetime</code> instance.\nTakes the same extra arguments as <a class=\"reference internal\" href=\"#django.db.models.DateField\" title=\"django.db.models.DateField\"><code class=\"xref py py-class docutils literal notranslate\">DateField</code></a>.</p>\n<p>The default form widget for this field is a single\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>. The admin uses two separate\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a> widgets with JavaScript shortcuts.</p>\n</section>\n<section id=\"decimalfield\">\n<h3><code class=\"docutils literal notranslate\">DecimalField</code><a class=\"heading-anchor\" href=\"#decimalfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DecimalField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">DecimalField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_digits<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">decimal_places<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.DecimalField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A fixed-precision decimal number, represented in Python by a\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/decimal.html#decimal.Decimal\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">Decimal</code></a> instance. It validates the input using\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.DecimalValidator\" title=\"django.core.validators.DecimalValidator\"><code class=\"xref py py-class docutils literal notranslate\">DecimalValidator</code></a>.</p>\n<p>Has two <strong>required</strong> arguments:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DecimalField.max_digits\">\n<span class=\"sig-prename descclassname\">DecimalField.</span><span class=\"sig-name descname\">max_digits</span><a class=\"heading-anchor\" href=\"#django.db.models.DecimalField.max_digits\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The maximum number of digits allowed in the number. Note that this number\nmust be greater than or equal to <code class=\"docutils literal notranslate\">decimal_places</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DecimalField.decimal_places\">\n<span class=\"sig-prename descclassname\">DecimalField.</span><span class=\"sig-name descname\">decimal_places</span><a class=\"heading-anchor\" href=\"#django.db.models.DecimalField.decimal_places\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The number of decimal places to store with the number.</p>\n</dd></dl>\n\n<p>For example, to store numbers up to <code class=\"docutils literal notranslate\">999</code> with a resolution of 2 decimal\nplaces, you'd use:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>models<span class=\"o\">.</span>DecimalField<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> max_digits<span class=\"o\">=</span><span class=\"mi\">5</span><span class=\"p\">,</span> decimal_places<span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>And to store numbers up to approximately one billion with a resolution of 10\ndecimal places:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>models<span class=\"o\">.</span>DecimalField<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> max_digits<span class=\"o\">=</span><span class=\"mi\">19</span><span class=\"p\">,</span> decimal_places<span class=\"o\">=</span><span class=\"mi\">10</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.NumberInput\" title=\"django.forms.NumberInput\"><code class=\"xref py py-class docutils literal notranslate\">NumberInput</code></a>\nwhen <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.Field.localize\" title=\"django.forms.Field.localize\"><code class=\"xref py py-attr docutils literal notranslate\">localize</code></a> is <code class=\"docutils literal notranslate\">False</code> or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a> otherwise.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>For more information about the differences between the\n<a class=\"reference internal\" href=\"#django.db.models.FloatField\" title=\"django.db.models.FloatField\"><code class=\"xref py py-class docutils literal notranslate\">FloatField</code></a> and <a class=\"reference internal\" href=\"#django.db.models.DecimalField\" title=\"django.db.models.DecimalField\"><code class=\"xref py py-class docutils literal notranslate\">DecimalField</code></a> classes, please\nsee <a class=\"reference internal\" href=\"#floatfield-vs-decimalfield\"><span class=\"std std-ref\">FloatField vs. DecimalField</span></a>. You\nshould also be aware of <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/databases/#sqlite-decimal-handling\"><span class=\"std std-ref\">SQLite limitations</span></a>\nof decimal fields.</p>\n</aside>\n</section>\n<section id=\"durationfield\">\n<h3><code class=\"docutils literal notranslate\">DurationField</code><a class=\"heading-anchor\" href=\"#durationfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DurationField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">DurationField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.DurationField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A field for storing periods of time - modeled in Python by\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.timedelta\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">timedelta</code></a>. When used on PostgreSQL, the data type\nused is an <code class=\"docutils literal notranslate\">interval</code> and on Oracle the data type is <code class=\"docutils literal notranslate\">INTERVAL DAY(9) TO\nSECOND(6)</code>. Otherwise a <code class=\"docutils literal notranslate\">bigint</code> of microseconds is used.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Arithmetic with <code class=\"docutils literal notranslate\">DurationField</code> works in most cases. However on all\ndatabases other than PostgreSQL, comparing the value of a <code class=\"docutils literal notranslate\">DurationField</code>\nto arithmetic on <code class=\"docutils literal notranslate\">DateTimeField</code> instances will not work as expected.</p>\n</aside>\n</section>\n<section id=\"emailfield\">\n<h3><code class=\"docutils literal notranslate\">EmailField</code><a class=\"heading-anchor\" href=\"#emailfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.EmailField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">EmailField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">254</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.EmailField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> that checks that the value is a valid email address using\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.EmailValidator\" title=\"django.core.validators.EmailValidator\"><code class=\"xref py py-class docutils literal notranslate\">EmailValidator</code></a>.</p>\n</section>\n<section id=\"filefield\">\n<h3><code class=\"docutils literal notranslate\">FileField</code><a class=\"heading-anchor\" href=\"#filefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FileField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">FileField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">upload_to<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">100</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.FileField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A file-upload field.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The <code class=\"docutils literal notranslate\">primary_key</code> argument isn't supported and will raise an error if\nused.</p>\n</aside>\n<p>Has two optional arguments:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FileField.upload_to\">\n<span class=\"sig-prename descclassname\">FileField.</span><span class=\"sig-name descname\">upload_to</span><a class=\"heading-anchor\" href=\"#django.db.models.FileField.upload_to\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This attribute provides a way of setting the upload directory and file name,\nand can be set in two ways. In both cases, the value is passed to the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage.save\" title=\"django.core.files.storage.Storage.save\"><code class=\"xref py py-meth docutils literal notranslate\">Storage.save()</code></a> method.</p>\n<p>If you specify a string value, it may contain <a class=\"reference external\" href=\"https://docs.python.org/3/library/time.html#time.strftime\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">strftime()</code></a>\nformatting, which will be replaced by the date/time of the file upload (so\nthat uploaded files don't fill up the given directory). For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">MyModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># file will be uploaded to MEDIA_ROOT/uploads</span>\n    upload <span class=\"o\">=</span> models<span class=\"o\">.</span>FileField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span><span class=\"s1\">&#39;uploads/&#39;</span><span class=\"p\">)</span>\n    <span class=\"c1\"># or...</span>\n    <span class=\"c1\"># file will be saved to MEDIA_ROOT/uploads/2015/01/30</span>\n    upload <span class=\"o\">=</span> models<span class=\"o\">.</span>FileField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span><span class=\"s1\">&#39;uploads/%Y/%m/</span><span class=\"si\">%d</span><span class=\"s1\">/&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>If you are using the default\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.FileSystemStorage\" title=\"django.core.files.storage.FileSystemStorage\"><code class=\"xref py py-class docutils literal notranslate\">FileSystemStorage</code></a>, the string value\nwill be appended to your <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> path to form the location on\nthe local filesystem where uploaded files will be stored. If you are using\na different storage, check that storage's documentation to see how it\nhandles <code class=\"docutils literal notranslate\">upload_to</code>.</p>\n<p><code class=\"docutils literal notranslate\">upload_to</code> may also be a callable, such as a function. This will be\ncalled to obtain the upload path, including the filename. This callable must\naccept two arguments and return a Unix-style path (with forward slashes)\nto be passed along to the storage system. The two arguments are:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Argument</p></th>\n<th class=\"head\"><p>描述</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><code class=\"docutils literal notranslate\">instance</code></p></td>\n<td><p>An instance of the model where the\n<code class=\"docutils literal notranslate\">FileField</code> is defined. More specifically,\nthis is the particular instance where the\ncurrent file is being attached.</p>\n<p>In most cases, this object will not have been\nsaved to the database yet, so if it uses the\ndefault <code class=\"docutils literal notranslate\">AutoField</code>, <em>it might not yet have a\nvalue for its primary key field</em>.</p>\n</td>\n</tr>\n<tr class=\"row-odd\"><td><p><code class=\"docutils literal notranslate\">filename</code></p></td>\n<td><p>The filename that was originally given to the\nfile. This may or may not be taken into account\nwhen determining the final destination path.</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>例子:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">user_directory_path</span><span class=\"p\">(</span>instance<span class=\"p\">,</span> filename<span class=\"p\">):</span>\n    <span class=\"c1\"># file will be uploaded to MEDIA_ROOT/user_&lt;id&gt;/&lt;filename&gt;</span>\n    <span class=\"k\">return</span> <span class=\"s1\">&#39;user_</span><span class=\"si\">{0}</span><span class=\"s1\">/</span><span class=\"si\">{1}</span><span class=\"s1\">&#39;</span><span class=\"o\">.</span>format<span class=\"p\">(</span>instance<span class=\"o\">.</span>user<span class=\"o\">.</span>id<span class=\"p\">,</span> filename<span class=\"p\">)</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">MyModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    upload <span class=\"o\">=</span> models<span class=\"o\">.</span>FileField<span class=\"p\">(</span>upload_to<span class=\"o\">=</span>user_directory_path<span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FileField.storage\">\n<span class=\"sig-prename descclassname\">FileField.</span><span class=\"sig-name descname\">storage</span><a class=\"heading-anchor\" href=\"#django.db.models.FileField.storage\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A storage object, which handles the storage and retrieval of your\nfiles. See <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/files/\"><span class=\"doc\">管理文件</span></a> for details on how to provide this object.</p>\n</dd></dl>\n\n<p>The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.ClearableFileInput\" title=\"django.forms.ClearableFileInput\"><code class=\"xref py py-class docutils literal notranslate\">ClearableFileInput</code></a>.</p>\n<p>Using a <a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> or an <a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> (see below) in a model\ntakes a few steps:</p>\n<ol class=\"arabic simple\">\n<li><p>在你的 setting 文件中，你需要定义：setting: MEDIA_ROOT 作为 Django 存储上传文件目录的完整路径。（为了提高性能，这些文件不会储存在数据库中）定义： setting: MEDIA_URL 作为该目录的基本公共 URL， 确保该目录能够被 Web 服务器的是用户写入。</p></li>\n<li><p>Add the <a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> or <a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> to your model, defining\nthe <a class=\"reference internal\" href=\"#django.db.models.FileField.upload_to\" title=\"django.db.models.FileField.upload_to\"><code class=\"xref py py-attr docutils literal notranslate\">upload_to</code></a> option to specify a subdirectory of\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> to use for uploaded files.</p></li>\n<li><p>All that will be stored in your database is a path to the file\n(relative to <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a>). You'll most likely want to use the\nconvenience <a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile.url\" title=\"django.db.models.fields.files.FieldFile.url\"><code class=\"xref py py-attr docutils literal notranslate\">url</code></a> attribute\nprovided by Django. For example, if your <a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> is called\n<code class=\"docutils literal notranslate\">mug_shot</code>, you can get the absolute path to your image in a template with\n<code class=\"docutils literal notranslate\">{{ object.mug_shot.url }}</code>.</p></li>\n</ol>\n<p>For example, say your <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_ROOT</code></a> is set to <code class=\"docutils literal notranslate\">'/home/media'</code>, and\n<a class=\"reference internal\" href=\"#django.db.models.FileField.upload_to\" title=\"django.db.models.FileField.upload_to\"><code class=\"xref py py-attr docutils literal notranslate\">upload_to</code></a> is set to <code class=\"docutils literal notranslate\">'photos/%Y/%m/%d'</code>. The <code class=\"docutils literal notranslate\">'%Y/%m/%d'</code>\npart of <a class=\"reference internal\" href=\"#django.db.models.FileField.upload_to\" title=\"django.db.models.FileField.upload_to\"><code class=\"xref py py-attr docutils literal notranslate\">upload_to</code></a> is <a class=\"reference external\" href=\"https://docs.python.org/3/library/time.html#time.strftime\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\">strftime()</code></a> formatting;\n<code class=\"docutils literal notranslate\">'%Y'</code> is the four-digit year, <code class=\"docutils literal notranslate\">'%m'</code> is the two-digit month and <code class=\"docutils literal notranslate\">'%d'</code> is\nthe two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in\nthe directory <code class=\"docutils literal notranslate\">/home/media/photos/2007/01/15</code>.</p>\n<p>If you wanted to retrieve the uploaded file's on-disk filename, or the file's\nsize, you could use the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File.name\" title=\"django.core.files.File.name\"><code class=\"xref py py-attr docutils literal notranslate\">name</code></a> and\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File.size\" title=\"django.core.files.File.size\"><code class=\"xref py py-attr docutils literal notranslate\">size</code></a> attributes respectively; for more\ninformation on the available attributes and methods, see the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> class reference and the <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/files/\"><span class=\"doc\">管理文件</span></a>\ntopic guide.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>文件在数据库中作为保存模型的一部分，因此在模型被保存之前，不能依赖磁盘上使用的实际文件名。</p>\n</aside>\n<p>The uploaded file's relative URL can be obtained using the\n<a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile.url\" title=\"django.db.models.fields.files.FieldFile.url\"><code class=\"xref py py-attr docutils literal notranslate\">url</code></a> attribute. Internally,\nthis calls the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage.url\" title=\"django.core.files.storage.Storage.url\"><code class=\"xref py py-meth docutils literal notranslate\">url()</code></a> method of the\nunderlying <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage\" title=\"django.core.files.storage.Storage\"><code class=\"xref py py-class docutils literal notranslate\">Storage</code></a> class.</p>\n<p id=\"file-upload-security\">Note that whenever you deal with uploaded files, you should pay close attention\nto where you're uploading them and what type of files they are, to avoid\nsecurity holes. <em>Validate all uploaded files</em> so that you're sure the files are\nwhat you think they are. For example, if you blindly let somebody upload files,\nwithout validation, to a directory that's within your Web server's document\nroot, then somebody could upload a CGI or PHP script and execute that script by\nvisiting its URL on your site. Don't allow that.</p>\n<p>Also note that even an uploaded HTML file, since it can be executed by the\nbrowser (though not by the server), can pose security threats that are\nequivalent to XSS or CSRF attacks.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> instances are created in your database as <code class=\"docutils literal notranslate\">varchar</code>\ncolumns with a default max length of 100 characters. As with other fields, you\ncan change the maximum length using the <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> argument.</p>\n<section id=\"filefield-and-fieldfile\">\n<h4><code class=\"docutils literal notranslate\">FileField``和``FieldFile</code><a class=\"heading-anchor\" href=\"#filefield-and-fieldfile\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">FieldFile</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>When you access a <a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> on a model, you are\ngiven an instance of <a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile\" title=\"django.db.models.fields.files.FieldFile\"><code class=\"xref py py-class docutils literal notranslate\">FieldFile</code></a> as a proxy for accessing the underlying\nfile.</p>\n<p>The API of <a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile\" title=\"django.db.models.fields.files.FieldFile\"><code class=\"xref py py-class docutils literal notranslate\">FieldFile</code></a> mirrors that of <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a>,\nwith one key difference: <em>The object wrapped by the class is not necessarily a\nwrapper around Python's built-in file object.</em> Instead, it is a wrapper around\nthe result of the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage.open\" title=\"django.core.files.storage.Storage.open\"><code class=\"xref py py-attr docutils literal notranslate\">Storage.open()</code></a>\nmethod, which may be a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> object, or it may be a\ncustom storage's implementation of the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> API.</p>\n<p>In addition to the API inherited from <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> such as\n<code class=\"docutils literal notranslate\">read()</code> and <code class=\"docutils literal notranslate\">write()</code>, <a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile\" title=\"django.db.models.fields.files.FieldFile\"><code class=\"xref py py-class docutils literal notranslate\">FieldFile</code></a> includes several methods that\ncan be used to interact with the underlying file:</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Two methods of this class, <a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile.save\" title=\"django.db.models.fields.files.FieldFile.save\"><code class=\"xref py py-meth docutils literal notranslate\">save()</code></a> and\n<a class=\"reference internal\" href=\"#django.db.models.fields.files.FieldFile.delete\" title=\"django.db.models.fields.files.FieldFile.delete\"><code class=\"xref py py-meth docutils literal notranslate\">delete()</code></a>, default to saving the model object of the\nassociated <code class=\"docutils literal notranslate\">FieldFile</code> in the database.</p>\n</aside>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.name\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the file including the relative path from the root of the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage\" title=\"django.core.files.storage.Storage\"><code class=\"xref py py-class docutils literal notranslate\">Storage</code></a> of the associated\n<a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.size\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">size</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.size\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The result of the underlying <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage.size\" title=\"django.core.files.storage.Storage.size\"><code class=\"xref py py-attr docutils literal notranslate\">Storage.size()</code></a> method.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.url\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">url</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.url\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A read-only property to access the file's relative URL by calling the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage.url\" title=\"django.core.files.storage.Storage.url\"><code class=\"xref py py-meth docutils literal notranslate\">url()</code></a> method of the underlying\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/storage/#django.core.files.storage.Storage\" title=\"django.core.files.storage.Storage\"><code class=\"xref py py-class docutils literal notranslate\">Storage</code></a> class.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.open\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">open</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">mode<span class=\"o\">=</span><span class=\"default_value\">'rb'</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.open\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Opens or reopens the file associated with this instance in the specified\n<code class=\"docutils literal notranslate\">mode</code>. Unlike the standard Python <code class=\"docutils literal notranslate\">open()</code> method, it doesn't return a\nfile descriptor.</p>\n<p>Since the underlying file is opened implicitly when accessing it, it may be\nunnecessary to call this method except to reset the pointer to the underlying\nfile or to change the <code class=\"docutils literal notranslate\">mode</code>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.close\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">close</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.close\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Behaves like the standard Python <code class=\"docutils literal notranslate\">file.close()</code> method and closes the file\nassociated with this instance.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.save\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">save</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">content</em>, <em class=\"sig-param\">save<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.save\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This method takes a filename and file contents and passes them to the storage\nclass for the field, then associates the stored file with the model field.\nIf you want to manually associate file data with\n<a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a> instances on your model, the <code class=\"docutils literal notranslate\">save()</code>\nmethod is used to persist that file data.</p>\n<p>Takes two required arguments: <code class=\"docutils literal notranslate\">name</code> which is the name of the file, and\n<code class=\"docutils literal notranslate\">content</code> which is an object containing the file's contents.  The\noptional <code class=\"docutils literal notranslate\">save</code> argument controls whether or not the model instance is\nsaved after the file associated with this field has been altered. Defaults to\n<code class=\"docutils literal notranslate\">True</code>.</p>\n<p>Note that the <code class=\"docutils literal notranslate\">content</code> argument should be an instance of\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">django.core.files.File</code></a>, not Python's built-in file object.\nYou can construct a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/files/file/#django.core.files.File\" title=\"django.core.files.File\"><code class=\"xref py py-class docutils literal notranslate\">File</code></a> from an existing\nPython file object like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.files</span> <span class=\"kn\">import</span> File\n<span class=\"c1\"># Open an existing file using Python&#39;s built-in open()</span>\nf <span class=\"o\">=</span> <span class=\"nb\">open</span><span class=\"p\">(</span><span class=\"s1\">&#39;/path/to/hello.world&#39;</span><span class=\"p\">)</span>\nmyfile <span class=\"o\">=</span> File<span class=\"p\">(</span>f<span class=\"p\">)</span>\n</code></pre></div>\n<p>Or you can construct one from a Python string like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.core.files.base</span> <span class=\"kn\">import</span> ContentFile\nmyfile <span class=\"o\">=</span> ContentFile<span class=\"p\">(</span><span class=\"s2\">&quot;hello world&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>For more information, see <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/files/\"><span class=\"doc\">管理文件</span></a>.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.fields.files.FieldFile.delete\">\n<span class=\"sig-prename descclassname\">FieldFile.</span><span class=\"sig-name descname\">delete</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">save<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.fields.files.FieldFile.delete\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Deletes the file associated with this instance and clears all attributes on\nthe field. Note: This method will close the file if it happens to be open when\n<code class=\"docutils literal notranslate\">delete()</code> is called.</p>\n<p>The optional <code class=\"docutils literal notranslate\">save</code> argument controls whether or not the model instance is\nsaved after the file associated with this field has been deleted. Defaults to\n<code class=\"docutils literal notranslate\">True</code>.</p>\n<p>Note that when a model is deleted, related files are not deleted. If you need\nto cleanup orphaned files, you'll need to handle it yourself (for instance,\nwith a custom management command that can be run manually or scheduled to run\nperiodically via e.g. cron).</p>\n</section>\n</section>\n<section id=\"filepathfield\">\n<h3><code class=\"docutils literal notranslate\">FilePathField</code><a class=\"heading-anchor\" href=\"#filepathfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">FilePathField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">match<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">recursive<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">100</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> whose choices are limited to the filenames in a certain\ndirectory on the filesystem. Has three special arguments, of which the first is\n<strong>required</strong>:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField.path\">\n<span class=\"sig-prename descclassname\">FilePathField.</span><span class=\"sig-name descname\">path</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField.path\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Required. The absolute filesystem path to a directory from which this\n<a class=\"reference internal\" href=\"#django.db.models.FilePathField\" title=\"django.db.models.FilePathField\"><code class=\"xref py py-class docutils literal notranslate\">FilePathField</code></a> should get its choices. Example: <code class=\"docutils literal notranslate\">&quot;/home/images&quot;</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField.match\">\n<span class=\"sig-prename descclassname\">FilePathField.</span><span class=\"sig-name descname\">match</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField.match\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional. A regular expression, as a string, that <a class=\"reference internal\" href=\"#django.db.models.FilePathField\" title=\"django.db.models.FilePathField\"><code class=\"xref py py-class docutils literal notranslate\">FilePathField</code></a>\nwill use to filter filenames. Note that the regex will be applied to the\nbase filename, not the full path. Example: <code class=\"docutils literal notranslate\">&quot;foo.*\\.txt$&quot;</code>, which will\nmatch a file called <code class=\"docutils literal notranslate\">foo23.txt</code> but not <code class=\"docutils literal notranslate\">bar.txt</code> or <code class=\"docutils literal notranslate\">foo23.png</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField.recursive\">\n<span class=\"sig-prename descclassname\">FilePathField.</span><span class=\"sig-name descname\">recursive</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField.recursive\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional. Either <code class=\"docutils literal notranslate\">True</code> or <code class=\"docutils literal notranslate\">False</code>. Default is <code class=\"docutils literal notranslate\">False</code>. Specifies\nwhether all subdirectories of <a class=\"reference internal\" href=\"#django.db.models.FilePathField.path\" title=\"django.db.models.FilePathField.path\"><code class=\"xref py py-attr docutils literal notranslate\">path</code></a> should be included</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField.allow_files\">\n<span class=\"sig-prename descclassname\">FilePathField.</span><span class=\"sig-name descname\">allow_files</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField.allow_files\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional.  Either <code class=\"docutils literal notranslate\">True</code> or <code class=\"docutils literal notranslate\">False</code>.  Default is <code class=\"docutils literal notranslate\">True</code>.  Specifies\nwhether files in the specified location should be included.  Either this or\n<a class=\"reference internal\" href=\"#django.db.models.FilePathField.allow_folders\" title=\"django.db.models.FilePathField.allow_folders\"><code class=\"xref py py-attr docutils literal notranslate\">allow_folders</code></a> must be <code class=\"docutils literal notranslate\">True</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FilePathField.allow_folders\">\n<span class=\"sig-prename descclassname\">FilePathField.</span><span class=\"sig-name descname\">allow_folders</span><a class=\"heading-anchor\" href=\"#django.db.models.FilePathField.allow_folders\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Optional.  Either <code class=\"docutils literal notranslate\">True</code> or <code class=\"docutils literal notranslate\">False</code>.  Default is <code class=\"docutils literal notranslate\">False</code>.  Specifies\nwhether folders in the specified location should be included.  Either this\nor <a class=\"reference internal\" href=\"#django.db.models.FilePathField.allow_files\" title=\"django.db.models.FilePathField.allow_files\"><code class=\"xref py py-attr docutils literal notranslate\">allow_files</code></a> must be <code class=\"docutils literal notranslate\">True</code>.</p>\n</dd></dl>\n\n<p>当然，这些参数能够同时使用。</p>\n<p>The one potential gotcha is that <a class=\"reference internal\" href=\"#django.db.models.FilePathField.match\" title=\"django.db.models.FilePathField.match\"><code class=\"xref py py-attr docutils literal notranslate\">match</code></a> applies to the\nbase filename, not the full path. So, this example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>FilePathField<span class=\"p\">(</span>path<span class=\"o\">=</span><span class=\"s2\">&quot;/home/images&quot;</span><span class=\"p\">,</span> match<span class=\"o\">=</span><span class=\"s2\">&quot;foo.*&quot;</span><span class=\"p\">,</span> recursive<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>...will match <code class=\"docutils literal notranslate\">/home/images/foo.png</code> but not <code class=\"docutils literal notranslate\">/home/images/foo/bar.png</code>\nbecause the <a class=\"reference internal\" href=\"#django.db.models.FilePathField.match\" title=\"django.db.models.FilePathField.match\"><code class=\"xref py py-attr docutils literal notranslate\">match</code></a> applies to the base filename\n(<code class=\"docutils literal notranslate\">foo.png</code> and <code class=\"docutils literal notranslate\">bar.png</code>).</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.FilePathField\" title=\"django.db.models.FilePathField\"><code class=\"xref py py-class docutils literal notranslate\">FilePathField</code></a> instances are created in your database as <code class=\"docutils literal notranslate\">varchar</code>\ncolumns with a default max length of 100 characters. As with other fields, you\ncan change the maximum length using the <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> argument.</p>\n</section>\n<section id=\"floatfield\">\n<h3><code class=\"docutils literal notranslate\">FloatField</code><a class=\"heading-anchor\" href=\"#floatfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.FloatField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">FloatField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.FloatField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A floating-point number represented in Python by a <code class=\"docutils literal notranslate\">float</code> instance.</p>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.NumberInput\" title=\"django.forms.NumberInput\"><code class=\"xref py py-class docutils literal notranslate\">NumberInput</code></a>\nwhen <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.Field.localize\" title=\"django.forms.Field.localize\"><code class=\"xref py py-attr docutils literal notranslate\">localize</code></a> is <code class=\"docutils literal notranslate\">False</code> or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a> otherwise.</p>\n<aside class=\"admonition-floatfield-vs-decimalfield admonition\" id=\"floatfield-vs-decimalfield\" role=\"note\">\n<p class=\"admonition-title\"><code class=\"docutils literal notranslate\">FloatField</code> vs. <code class=\"docutils literal notranslate\">DecimalField</code></p>\n<p>The <a class=\"reference internal\" href=\"#django.db.models.FloatField\" title=\"django.db.models.FloatField\"><code class=\"xref py py-class docutils literal notranslate\">FloatField</code></a> class is sometimes mixed up with the\n<a class=\"reference internal\" href=\"#django.db.models.DecimalField\" title=\"django.db.models.DecimalField\"><code class=\"xref py py-class docutils literal notranslate\">DecimalField</code></a> class. Although they both represent real numbers, they\nrepresent those numbers differently. <code class=\"docutils literal notranslate\">FloatField</code> uses Python's <code class=\"docutils literal notranslate\">float</code>\ntype internally, while <code class=\"docutils literal notranslate\">DecimalField</code> uses Python's <code class=\"docutils literal notranslate\">Decimal</code> type. For\ninformation on the difference between the two, see Python's documentation\nfor the <a class=\"reference external\" href=\"https://docs.python.org/3/library/decimal.html#module-decimal\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">decimal</code></a> module.</p>\n</aside>\n</section>\n<section id=\"imagefield\">\n<h3><code class=\"docutils literal notranslate\">ImageField</code><a class=\"heading-anchor\" href=\"#imagefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ImageField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ImageField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">upload_to<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">height_field<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">width_field<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">100</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.ImageField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Inherits all attributes and methods from <a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a>, but also\nvalidates that the uploaded object is a valid image.</p>\n<p>In addition to the special attributes that are available for <a class=\"reference internal\" href=\"#django.db.models.FileField\" title=\"django.db.models.FileField\"><code class=\"xref py py-class docutils literal notranslate\">FileField</code></a>,\nan <a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> also has <code class=\"docutils literal notranslate\">height</code> and <code class=\"docutils literal notranslate\">width</code> attributes.</p>\n<p>To facilitate querying on those attributes, <a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> has two extra\noptional arguments:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ImageField.height_field\">\n<span class=\"sig-prename descclassname\">ImageField.</span><span class=\"sig-name descname\">height_field</span><a class=\"heading-anchor\" href=\"#django.db.models.ImageField.height_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Name of a model field which will be auto-populated with the height of the\nimage each time the model instance is saved.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ImageField.width_field\">\n<span class=\"sig-prename descclassname\">ImageField.</span><span class=\"sig-name descname\">width_field</span><a class=\"heading-anchor\" href=\"#django.db.models.ImageField.width_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Name of a model field which will be auto-populated with the width of the\nimage each time the model instance is saved.</p>\n</dd></dl>\n\n<p>Requires the <a class=\"reference external\" href=\"https://pillow.readthedocs.io/en/latest/\">Pillow</a> library.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\">ImageField</code></a> instances are created in your database as <code class=\"docutils literal notranslate\">varchar</code>\ncolumns with a default max length of 100 characters. As with other fields, you\ncan change the maximum length using the <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> argument.</p>\n<p>The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.ClearableFileInput\" title=\"django.forms.ClearableFileInput\"><code class=\"xref py py-class docutils literal notranslate\">ClearableFileInput</code></a>.</p>\n</section>\n<section id=\"integerfield\">\n<h3><code class=\"docutils literal notranslate\">IntegerField</code><a class=\"heading-anchor\" href=\"#integerfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.IntegerField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">IntegerField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.IntegerField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>An integer. Values from <code class=\"docutils literal notranslate\">-2147483648</code> to <code class=\"docutils literal notranslate\">2147483647</code> are safe in all\ndatabases supported by Django.</p>\n<p>It uses <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.MinValueValidator\" title=\"django.core.validators.MinValueValidator\"><code class=\"xref py py-class docutils literal notranslate\">MinValueValidator</code></a> and\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.MaxValueValidator\" title=\"django.core.validators.MaxValueValidator\"><code class=\"xref py py-class docutils literal notranslate\">MaxValueValidator</code></a> to validate the input based\non the values that the default database supports.</p>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.NumberInput\" title=\"django.forms.NumberInput\"><code class=\"xref py py-class docutils literal notranslate\">NumberInput</code></a>\nwhen <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.Field.localize\" title=\"django.forms.Field.localize\"><code class=\"xref py py-attr docutils literal notranslate\">localize</code></a> is <code class=\"docutils literal notranslate\">False</code> or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a> otherwise.</p>\n</section>\n<section id=\"genericipaddressfield\">\n<h3><code class=\"docutils literal notranslate\">GenericIPAddressField</code><a class=\"heading-anchor\" href=\"#genericipaddressfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.GenericIPAddressField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericIPAddressField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">protocol<span class=\"o\">=</span><span class=\"default_value\">'both'</span></em>, <em class=\"sig-param\">unpack_ipv4<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.GenericIPAddressField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>An IPv4 or IPv6 address, in string format (e.g. <code class=\"docutils literal notranslate\">192.0.2.30</code> or\n<code class=\"docutils literal notranslate\">2a02:42fe::4</code>). The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>.</p>\n<p>The IPv6 address normalization follows <span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc4291.html#section-2.2\"><strong>RFC 4291 Section 2.2</strong></a> section 2.2,\nincluding using the IPv4 format suggested in paragraph 3 of that section, like\n<code class=\"docutils literal notranslate\">::ffff:192.0.2.0</code>. For example, <code class=\"docutils literal notranslate\">2001:0::0:01</code> would be normalized to\n<code class=\"docutils literal notranslate\">2001::1</code>, and <code class=\"docutils literal notranslate\">::ffff:0a0a:0a0a</code> to <code class=\"docutils literal notranslate\">::ffff:10.10.10.10</code>. All characters\nare converted to lowercase.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.GenericIPAddressField.protocol\">\n<span class=\"sig-prename descclassname\">GenericIPAddressField.</span><span class=\"sig-name descname\">protocol</span><a class=\"heading-anchor\" href=\"#django.db.models.GenericIPAddressField.protocol\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Limits valid inputs to the specified protocol.\nAccepted values are <code class=\"docutils literal notranslate\">'both'</code> (default), <code class=\"docutils literal notranslate\">'IPv4'</code>\nor <code class=\"docutils literal notranslate\">'IPv6'</code>. Matching is case insensitive.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.GenericIPAddressField.unpack_ipv4\">\n<span class=\"sig-prename descclassname\">GenericIPAddressField.</span><span class=\"sig-name descname\">unpack_ipv4</span><a class=\"heading-anchor\" href=\"#django.db.models.GenericIPAddressField.unpack_ipv4\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Unpacks IPv4 mapped addresses like <code class=\"docutils literal notranslate\">::ffff:192.0.2.1</code>.\nIf this option is enabled that address would be unpacked to\n<code class=\"docutils literal notranslate\">192.0.2.1</code>. Default is disabled. Can only be used\nwhen <code class=\"docutils literal notranslate\">protocol</code> is set to <code class=\"docutils literal notranslate\">'both'</code>.</p>\n</dd></dl>\n\n<p>If you allow for blank values, you have to allow for null values since blank\nvalues are stored as null.</p>\n</section>\n<section id=\"nullbooleanfield\">\n<h3><code class=\"docutils literal notranslate\">NullBooleanField</code><a class=\"heading-anchor\" href=\"#nullbooleanfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.NullBooleanField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">NullBooleanField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.NullBooleanField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like <a class=\"reference internal\" href=\"#django.db.models.BooleanField\" title=\"django.db.models.BooleanField\"><code class=\"xref py py-class docutils literal notranslate\">BooleanField</code></a> with <code class=\"docutils literal notranslate\">null=True</code>. Use that instead of this field\nas it's likely to be deprecated in a future version of Django.</p>\n</section>\n<section id=\"positiveintegerfield\">\n<h3><code class=\"docutils literal notranslate\">PositiveIntegerField</code><a class=\"heading-anchor\" href=\"#positiveintegerfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.PositiveIntegerField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">PositiveIntegerField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.PositiveIntegerField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like an <a class=\"reference internal\" href=\"#django.db.models.IntegerField\" title=\"django.db.models.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\">IntegerField</code></a>, but must be either positive or zero (<code class=\"docutils literal notranslate\">0</code>).\nValues from <code class=\"docutils literal notranslate\">0</code> to <code class=\"docutils literal notranslate\">2147483647</code> are safe in all databases supported by\nDjango. The value <code class=\"docutils literal notranslate\">0</code> is accepted for backward compatibility reasons.</p>\n</section>\n<section id=\"positivesmallintegerfield\">\n<h3><code class=\"docutils literal notranslate\">PositiveSmallIntegerField</code><a class=\"heading-anchor\" href=\"#positivesmallintegerfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.PositiveSmallIntegerField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">PositiveSmallIntegerField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.PositiveSmallIntegerField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like a <a class=\"reference internal\" href=\"#django.db.models.PositiveIntegerField\" title=\"django.db.models.PositiveIntegerField\"><code class=\"xref py py-class docutils literal notranslate\">PositiveIntegerField</code></a>, but only allows values under a certain\n(database-dependent) point. Values from <code class=\"docutils literal notranslate\">0</code> to <code class=\"docutils literal notranslate\">32767</code> are safe in all\ndatabases supported by Django.</p>\n</section>\n<section id=\"slugfield\">\n<h3><code class=\"docutils literal notranslate\">SlugField</code><a class=\"heading-anchor\" href=\"#slugfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SlugField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SlugField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">50</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.SlugField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><a class=\"reference internal\" href=\"/zh-hans/2.2/glossary/#term-slug\"><span class=\"xref std std-term\">Slug</span></a> is a newspaper term. A slug is a short label for something,\ncontaining only letters, numbers, underscores or hyphens. They're generally used\nin URLs.</p>\n<p>Like a CharField, you can specify <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> (read the note\nabout database portability and <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> in that section,\ntoo). If <a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> is not specified, Django will use a\ndefault length of 50.</p>\n<p>Implies setting <a class=\"reference internal\" href=\"#django.db.models.Field.db_index\" title=\"django.db.models.Field.db_index\"><code class=\"xref py py-attr docutils literal notranslate\">Field.db_index</code></a> to <code class=\"docutils literal notranslate\">True</code>.</p>\n<p>It is often useful to automatically prepopulate a SlugField based on the value\nof some other value.  You can do this automatically in the admin using\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields\" title=\"django.contrib.admin.ModelAdmin.prepopulated_fields\"><code class=\"xref py py-attr docutils literal notranslate\">prepopulated_fields</code></a>.</p>\n<p>It uses <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.validate_slug\" title=\"django.core.validators.validate_slug\"><code class=\"xref py py-class docutils literal notranslate\">validate_slug</code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.validate_unicode_slug\" title=\"django.core.validators.validate_unicode_slug\"><code class=\"xref py py-class docutils literal notranslate\">validate_unicode_slug</code></a> for validation.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SlugField.allow_unicode\">\n<span class=\"sig-prename descclassname\">SlugField.</span><span class=\"sig-name descname\">allow_unicode</span><a class=\"heading-anchor\" href=\"#django.db.models.SlugField.allow_unicode\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>If <code class=\"docutils literal notranslate\">True</code>, the field accepts Unicode letters in addition to ASCII\nletters. Defaults to <code class=\"docutils literal notranslate\">False</code>.</p>\n</dd></dl>\n\n</section>\n<section id=\"smallintegerfield\">\n<h3><code class=\"docutils literal notranslate\">SmallIntegerField</code><a class=\"heading-anchor\" href=\"#smallintegerfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SmallIntegerField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">SmallIntegerField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.SmallIntegerField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Like an <a class=\"reference internal\" href=\"#django.db.models.IntegerField\" title=\"django.db.models.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\">IntegerField</code></a>, but only allows values under a certain\n(database-dependent) point. Values from <code class=\"docutils literal notranslate\">-32768</code> to <code class=\"docutils literal notranslate\">32767</code> are safe in all\ndatabases supported by Django.</p>\n</section>\n<section id=\"textfield\">\n<h3><code class=\"docutils literal notranslate\">TextField</code><a class=\"heading-anchor\" href=\"#textfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.TextField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">TextField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.TextField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A large text field. The default form widget for this field is a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.Textarea\" title=\"django.forms.Textarea\"><code class=\"xref py py-class docutils literal notranslate\">Textarea</code></a>.</p>\n<p>If you specify a <code class=\"docutils literal notranslate\">max_length</code> attribute, it will be reflected in the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.Textarea\" title=\"django.forms.Textarea\"><code class=\"xref py py-class docutils literal notranslate\">Textarea</code></a> widget of the auto-generated form field.\nHowever it is not enforced at the model or database level. Use a\n<a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> for that.</p>\n</section>\n<section id=\"timefield\">\n<h3><code class=\"docutils literal notranslate\">TimeField</code><a class=\"heading-anchor\" href=\"#timefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.TimeField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">TimeField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">auto_now<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">auto_now_add<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.TimeField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A time, represented in Python by a <code class=\"docutils literal notranslate\">datetime.time</code> instance. Accepts the same\nauto-population options as <a class=\"reference internal\" href=\"#django.db.models.DateField\" title=\"django.db.models.DateField\"><code class=\"xref py py-class docutils literal notranslate\">DateField</code></a>.</p>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>.\nThe admin adds some JavaScript shortcuts.</p>\n</section>\n<section id=\"urlfield\">\n<h3><code class=\"docutils literal notranslate\">URLField</code><a class=\"heading-anchor\" href=\"#urlfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.URLField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">URLField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">200</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.URLField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> for a URL, validated by\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/validators/#django.core.validators.URLValidator\" title=\"django.core.validators.URLValidator\"><code class=\"xref py py-class docutils literal notranslate\">URLValidator</code></a>.</p>\n<p>The default form widget for this field is a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/widgets/#django.forms.TextInput\" title=\"django.forms.TextInput\"><code class=\"xref py py-class docutils literal notranslate\">TextInput</code></a>.</p>\n<p>Like all <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> subclasses, <a class=\"reference internal\" href=\"#django.db.models.URLField\" title=\"django.db.models.URLField\"><code class=\"xref py py-class docutils literal notranslate\">URLField</code></a> takes the optional\n<a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a> argument. If you don't specify\n<a class=\"reference internal\" href=\"#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\">max_length</code></a>, a default of 200 is used.</p>\n</section>\n<section id=\"uuidfield\">\n<h3><code class=\"docutils literal notranslate\">UUIDField</code><a class=\"heading-anchor\" href=\"#uuidfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.UUIDField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">UUIDField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.UUIDField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A field for storing universally unique identifiers. Uses Python's\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/uuid.html#uuid.UUID\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\">UUID</code></a> class. When used on PostgreSQL, this stores in a\n<code class=\"docutils literal notranslate\">uuid</code> datatype, otherwise in a <code class=\"docutils literal notranslate\">char(32)</code>.</p>\n<p>Universally unique identifiers are a good alternative to <a class=\"reference internal\" href=\"#django.db.models.AutoField\" title=\"django.db.models.AutoField\"><code class=\"xref py py-class docutils literal notranslate\">AutoField</code></a> for\n<a class=\"reference internal\" href=\"#django.db.models.Field.primary_key\" title=\"django.db.models.Field.primary_key\"><code class=\"xref py py-attr docutils literal notranslate\">primary_key</code></a>. The database will not generate the UUID for you, so\nit is recommended to use <a class=\"reference internal\" href=\"#django.db.models.Field.default\" title=\"django.db.models.Field.default\"><code class=\"xref py py-attr docutils literal notranslate\">default</code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span> <span class=\"nn\">uuid</span>\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">MyUUIDModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"nb\">id</span> <span class=\"o\">=</span> models<span class=\"o\">.</span>UUIDField<span class=\"p\">(</span>primary_key<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span> default<span class=\"o\">=</span>uuid<span class=\"o\">.</span>uuid4<span class=\"p\">,</span> editable<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n    <span class=\"c1\"># other fields</span>\n</code></pre></div>\n<p>Note that a callable (with the parentheses omitted) is passed to <code class=\"docutils literal notranslate\">default</code>,\nnot an instance of <code class=\"docutils literal notranslate\">UUID</code>.</p>\n<aside class=\"admonition-lookups-on-postgresql admonition\" role=\"note\">\n<p class=\"admonition-title\">Lookups on PostgreSQL</p>\n<p>Using <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-iexact\"><code class=\"xref std std-lookup docutils literal notranslate\">iexact</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-contains\"><code class=\"xref std std-lookup docutils literal notranslate\">contains</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-icontains\"><code class=\"xref std std-lookup docutils literal notranslate\">icontains</code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-startswith\"><code class=\"xref std std-lookup docutils literal notranslate\">startswith</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-istartswith\"><code class=\"xref std std-lookup docutils literal notranslate\">istartswith</code></a>, <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-endswith\"><code class=\"xref std std-lookup docutils literal notranslate\">endswith</code></a>, or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#std-fieldlookup-iendswith\"><code class=\"xref std std-lookup docutils literal notranslate\">iendswith</code></a> lookups on PostgreSQL don't work for values without\nhyphens, because PostgreSQL stores them in a hyphenated uuid datatype type.</p>\n</aside>\n</section>\n</section>\n<section id=\"module-django.db.models.fields.related\">\n<span id=\"relationship-fields\"></span><h2>关联关系字段<a class=\"heading-anchor\" href=\"#module-django.db.models.fields.related\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django也定义了一组代表关系的字段。</p>\n<section id=\"foreignkey\">\n<span id=\"ref-foreignkey\"></span><h3><code class=\"docutils literal notranslate\">ForeignKey</code><a class=\"heading-anchor\" href=\"#foreignkey\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ForeignKey</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">to</em>, <em class=\"sig-param\">on_delete</em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>多对一的关系。需要两个位置参数：被关联的类和 <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.on_delete\" title=\"django.db.models.ForeignKey.on_delete\"><code class=\"xref py py-attr docutils literal notranslate\">on_delete</code></a> 选项</p>\n<p id=\"recursive-relationships\">如果要创建一个递归关系 -- 一个与其自身有多对一关系的对象 -- 则使用 <code class=\"docutils literal notranslate\">models.ForeignKey('self', on_delete=models.CASCADE)</code>。</p>\n<p id=\"lazy-relationships\">If you need to create a relationship on a model that has not yet been defined,\nyou can use the name of the model, rather than the model object itself:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Car</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    manufacturer <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        <span class=\"s1\">&#39;Manufacturer&#39;</span><span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    <span class=\"c1\"># ...</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">Manufacturer</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">pass</span>\n</code></pre></div>\n<p>Relationships defined this way on <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#abstract-base-classes\"><span class=\"std std-ref\">abstract models</span></a> are resolved when the model is subclassed as a\nconcrete model and are not relative to the abstract model's <code class=\"docutils literal notranslate\">app_label</code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">products/models.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">AbstractCar</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    manufacturer <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span><span class=\"s1\">&#39;Manufacturer&#39;</span><span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        abstract <span class=\"o\">=</span> <span class=\"kc\">True</span>\n</code></pre></figure>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\">production/models.py</figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">products.models</span> <span class=\"kn\">import</span> AbstractCar\n\n<span class=\"k\">class</span> <span class=\"nc\">Manufacturer</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    <span class=\"k\">pass</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">Car</span><span class=\"p\">(</span>AbstractCar<span class=\"p\">):</span>\n    <span class=\"k\">pass</span>\n\n<span class=\"c1\"># Car.manufacturer will point to `production.Manufacturer` here.</span>\n</code></pre></figure>\n<p>To refer to models defined in another application, you can explicitly specify\na model with the full application label. For example, if the <code class=\"docutils literal notranslate\">Manufacturer</code>\nmodel above is defined in another application called <code class=\"docutils literal notranslate\">production</code>, you'd\nneed to use:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span> <span class=\"nc\">Car</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    manufacturer <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        <span class=\"s1\">&#39;production.Manufacturer&#39;</span><span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n<p>This sort of reference, called a lazy relationship, can be useful when\nresolving circular import dependencies between two applications.</p>\n<p>A database index is automatically created on the <code class=\"docutils literal notranslate\">ForeignKey</code>. You can\ndisable this by setting <a class=\"reference internal\" href=\"#django.db.models.Field.db_index\" title=\"django.db.models.Field.db_index\"><code class=\"xref py py-attr docutils literal notranslate\">db_index</code></a> to <code class=\"docutils literal notranslate\">False</code>.  You may want to\navoid the overhead of an index if you are creating a foreign key for\nconsistency rather than joins, or if you will be creating an alternative index\nlike a partial or multiple column index.</p>\n<section id=\"database-representation\">\n<h4>数据库表现<a class=\"heading-anchor\" href=\"#database-representation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Behind the scenes, Django appends <code class=\"docutils literal notranslate\">&quot;_id&quot;</code> to the field name to create its\ndatabase column name. In the above example, the database table for the <code class=\"docutils literal notranslate\">Car</code>\nmodel will have a <code class=\"docutils literal notranslate\">manufacturer_id</code> column. (You can change this explicitly by\nspecifying <a class=\"reference internal\" href=\"#django.db.models.Field.db_column\" title=\"django.db.models.Field.db_column\"><code class=\"xref py py-attr docutils literal notranslate\">db_column</code></a>) However, your code should never have to\ndeal with the database column name, unless you write custom SQL. You'll always\ndeal with the field names of your model object.</p>\n</section>\n<section id=\"arguments\">\n<span id=\"foreign-key-arguments\"></span><h4>参数<a class=\"heading-anchor\" href=\"#arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> accepts other arguments that define the details of how the\nrelation works.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.on_delete\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">on_delete</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.on_delete\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>When an object referenced by a <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> is deleted, Django will\nemulate the behavior of the SQL constraint specified by the\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey.on_delete\" title=\"django.db.models.ForeignKey.on_delete\"><code class=\"xref py py-attr docutils literal notranslate\">on_delete</code></a> argument. For example, if you have a nullable\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> and you want it to be set null when the referenced\nobject is deleted:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>user <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n    User<span class=\"p\">,</span>\n    models<span class=\"o\">.</span>SET_NULL<span class=\"p\">,</span>\n    blank<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n    null<span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">on_delete</code> doesn't create a SQL constraint in the database. Support for\ndatabase-level cascade options <a class=\"extlink-ticket reference external\" href=\"https://code.djangoproject.com/ticket/21961\">may be implemented later</a>.</p>\n</dd></dl>\n\n<p>The possible values for <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.on_delete\" title=\"django.db.models.ForeignKey.on_delete\"><code class=\"xref py py-attr docutils literal notranslate\">on_delete</code></a> are found in\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#module-django.db.models\" title=\"django.db.models\"><code class=\"xref py py-mod docutils literal notranslate\">django.db.models</code></a>:</p>\n<ul>\n<li><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.CASCADE\">\n<span class=\"sig-name descname\">CASCADE</span><a class=\"heading-anchor\" href=\"#django.db.models.CASCADE\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Cascade deletes. Django emulates the behavior of the SQL constraint ON\nDELETE CASCADE and also deletes the object containing the ForeignKey.</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.delete\" title=\"django.db.models.Model.delete\"><code class=\"xref py py-meth docutils literal notranslate\">Model.delete()</code></a> isn't called on related models, but the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/signals/#django.db.models.signals.pre_delete\" title=\"django.db.models.signals.pre_delete\"><code class=\"xref py py-data docutils literal notranslate\">pre_delete</code></a> and\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/signals/#django.db.models.signals.post_delete\" title=\"django.db.models.signals.post_delete\"><code class=\"xref py py-data docutils literal notranslate\">post_delete</code></a> signals are sent for all\ndeleted objects.</p>\n</dd></dl>\n\n</li>\n<li><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.PROTECT\">\n<span class=\"sig-name descname\">PROTECT</span><a class=\"heading-anchor\" href=\"#django.db.models.PROTECT\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Prevent deletion of the referenced object by raising\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/exceptions/#django.db.models.ProtectedError\" title=\"django.db.models.ProtectedError\"><code class=\"xref py py-exc docutils literal notranslate\">ProtectedError</code></a>, a subclass of\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/exceptions/#django.db.IntegrityError\" title=\"django.db.IntegrityError\"><code class=\"xref py py-exc docutils literal notranslate\">django.db.IntegrityError</code></a>.</p>\n</dd></dl>\n\n</li>\n<li><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SET_NULL\">\n<span class=\"sig-name descname\">SET_NULL</span><a class=\"heading-anchor\" href=\"#django.db.models.SET_NULL\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Set the <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> null; this is only possible if\n<a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a> is <code class=\"docutils literal notranslate\">True</code>.</p>\n</dd></dl>\n\n</li>\n<li><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SET_DEFAULT\">\n<span class=\"sig-name descname\">SET_DEFAULT</span><a class=\"heading-anchor\" href=\"#django.db.models.SET_DEFAULT\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Set the <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> to its default value; a default for the\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> must be set.</p>\n</dd></dl>\n\n</li>\n<li><dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.SET\">\n<span class=\"sig-name descname\">SET</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.SET\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Set the <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> to the value passed to\n<a class=\"reference internal\" href=\"#django.db.models.SET\" title=\"django.db.models.SET\"><code class=\"xref py py-func docutils literal notranslate\">SET()</code></a>, or if a callable is passed in,\nthe result of calling it. In most cases, passing a callable will be\nnecessary to avoid executing queries at the time your models.py is\nimported:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth</span> <span class=\"kn\">import</span> get_user_model\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">def</span> <span class=\"nf\">get_sentinel_user</span><span class=\"p\">():</span>\n    <span class=\"k\">return</span> get_user_model<span class=\"p\">()</span><span class=\"o\">.</span>objects<span class=\"o\">.</span>get_or_create<span class=\"p\">(</span>username<span class=\"o\">=</span><span class=\"s1\">&#39;deleted&#39;</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">MyModel</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    user <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        settings<span class=\"o\">.</span>AUTH_USER_MODEL<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>SET<span class=\"p\">(</span>get_sentinel_user<span class=\"p\">),</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</li>\n<li><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.DO_NOTHING\">\n<span class=\"sig-name descname\">DO_NOTHING</span><a class=\"heading-anchor\" href=\"#django.db.models.DO_NOTHING\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Take no action. If your database backend enforces referential\nintegrity, this will cause an <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/exceptions/#django.db.IntegrityError\" title=\"django.db.IntegrityError\"><code class=\"xref py py-exc docutils literal notranslate\">IntegrityError</code></a> unless\nyou manually add an SQL <code class=\"docutils literal notranslate\">ON DELETE</code> constraint to the database field.</p>\n</dd></dl>\n\n</li>\n</ul>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.limit_choices_to\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">limit_choices_to</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.limit_choices_to\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sets a limit to the available choices for this field when this field is\nrendered using a <code class=\"docutils literal notranslate\">ModelForm</code> or the admin (by default, all objects\nin the queryset are available to choose). Either a dictionary, a\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#django.db.models.Q\" title=\"django.db.models.Q\"><code class=\"xref py py-class docutils literal notranslate\">Q</code></a> object, or a callable returning a\ndictionary or <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#django.db.models.Q\" title=\"django.db.models.Q\"><code class=\"xref py py-class docutils literal notranslate\">Q</code></a> object can be used.</p>\n<p>例子:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>staff_member <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n    User<span class=\"p\">,</span>\n    on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    limit_choices_to<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;is_staff&#39;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>causes the corresponding field on the <code class=\"docutils literal notranslate\">ModelForm</code> to list only <code class=\"docutils literal notranslate\">Users</code>\nthat have <code class=\"docutils literal notranslate\">is_staff=True</code>. This may be helpful in the Django admin.</p>\n<p>The callable form can be helpful, for instance, when used in conjunction\nwith the Python <code class=\"docutils literal notranslate\">datetime</code> module to limit selections by date range. For\nexample:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span> <span class=\"nf\">limit_pub_date_choices</span><span class=\"p\">():</span>\n    <span class=\"k\">return</span> <span class=\"p\">{</span><span class=\"s1\">&#39;pub_date__lte&#39;</span><span class=\"p\">:</span> datetime<span class=\"o\">.</span>date<span class=\"o\">.</span>utcnow<span class=\"p\">()}</span>\n\nlimit_choices_to <span class=\"o\">=</span> limit_pub_date_choices\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\">limit_choices_to</code> is or returns a <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#django.db.models.Q\" title=\"django.db.models.Q\"><code class=\"xref py py-class docutils literal notranslate\">Q object</code></a>, which is useful for <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/queries/#complex-lookups-with-q\"><span class=\"std std-ref\">complex queries</span></a>, then it will only have an effect on the choices\navailable in the admin when the field is not listed in\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields\" title=\"django.contrib.admin.ModelAdmin.raw_id_fields\"><code class=\"xref py py-attr docutils literal notranslate\">raw_id_fields</code></a> in the\n<code class=\"docutils literal notranslate\">ModelAdmin</code> for the model.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If a callable is used for <code class=\"docutils literal notranslate\">limit_choices_to</code>, it will be invoked\nevery time a new form is instantiated. It may also be invoked when a\nmodel is validated, for example by management commands or the admin.\nThe admin constructs querysets to validate its form inputs in various\nedge cases multiple times, so there is a possibility your callable may\nbe invoked several times.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.related_name\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">related_name</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.related_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name to use for the relation from the related object back to this one.\nIt's also the default value for <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_query_name\" title=\"django.db.models.ForeignKey.related_query_name\"><code class=\"xref py py-attr docutils literal notranslate\">related_query_name</code></a> (the name to use\nfor the reverse filter name from the target model). See the <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/queries/#backwards-related-objects\"><span class=\"std std-ref\">related\nobjects documentation</span></a> for a full explanation\nand example. Note that you must set this value when defining relations on\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#abstract-base-classes\"><span class=\"std std-ref\">abstract models</span></a>; and when you do so\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#abstract-related-name\"><span class=\"std std-ref\">some special syntax</span></a> is available.</p>\n<p>If you'd prefer Django not to create a backwards relation, set\n<code class=\"docutils literal notranslate\">related_name</code> to <code class=\"docutils literal notranslate\">'+'</code> or end it with <code class=\"docutils literal notranslate\">'+'</code>. For example, this will\nensure that the <code class=\"docutils literal notranslate\">User</code> model won't have a backwards relation to this\nmodel:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>user <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n    User<span class=\"p\">,</span>\n    on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    related_name<span class=\"o\">=</span><span class=\"s1\">&#39;+&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.related_query_name\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">related_query_name</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.related_query_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name to use for the reverse filter name from the target model. It\ndefaults to the value of <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_name\" title=\"django.db.models.ForeignKey.related_name\"><code class=\"xref py py-attr docutils literal notranslate\">related_name</code></a> or\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/options/#django.db.models.Options.default_related_name\" title=\"django.db.models.Options.default_related_name\"><code class=\"xref py py-attr docutils literal notranslate\">default_related_name</code></a> if set, otherwise it\ndefaults to the name of the model:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Declare the ForeignKey with related_query_name</span>\n<span class=\"k\">class</span> <span class=\"nc\">Tag</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    article <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        Article<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n        related_name<span class=\"o\">=</span><span class=\"s2\">&quot;tags&quot;</span><span class=\"p\">,</span>\n        related_query_name<span class=\"o\">=</span><span class=\"s2\">&quot;tag&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">255</span><span class=\"p\">)</span>\n\n<span class=\"c1\"># That&#39;s now the name of the reverse filter</span>\nArticle<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>tag__name<span class=\"o\">=</span><span class=\"s2\">&quot;important&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Like <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_name\" title=\"django.db.models.ForeignKey.related_name\"><code class=\"xref py py-attr docutils literal notranslate\">related_name</code></a>, <code class=\"docutils literal notranslate\">related_query_name</code> supports app label and\nclass interpolation via <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#abstract-related-name\"><span class=\"std std-ref\">some special syntax</span></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.to_field\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">to_field</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.to_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The field on the related object that the relation is to. By default, Django\nuses the primary key of the related object. If you reference a different\nfield, that field must have <code class=\"docutils literal notranslate\">unique=True</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.db_constraint\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">db_constraint</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.db_constraint\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>控制是否中介模型中外键的约束应该在数据库中被创建。默认值是``True``， 而且这几乎确定就是你需要的功能。将这个设为False会损坏数据的完整性。即便如此，一下场景一可能需要这样做</p>\n<ul class=\"simple\">\n<li><p>你有无效的冗余数据</p></li>\n<li><p>你正在共享你的数据库</p></li>\n</ul>\n<p>If this is set to <code class=\"docutils literal notranslate\">False</code>, accessing a related object that doesn't exist\nwill raise its <code class=\"docutils literal notranslate\">DoesNotExist</code> exception.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ForeignKey.swappable\">\n<span class=\"sig-prename descclassname\">ForeignKey.</span><span class=\"sig-name descname\">swappable</span><a class=\"heading-anchor\" href=\"#django.db.models.ForeignKey.swappable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Controls the migration framework's reaction if this <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>\nis pointing at a swappable model. If it is <code class=\"docutils literal notranslate\">True</code> - the default -\nthen if the <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> is pointing at a model which matches\nthe current value of <code class=\"docutils literal notranslate\">settings.AUTH_USER_MODEL</code> (or another swappable\nmodel setting) the relationship will be stored in the migration using\na reference to the setting, not to the model directly.</p>\n<p>You only want to override this to be <code class=\"docutils literal notranslate\">False</code> if you are sure your\nmodel should always point towards the swapped-in model - for example,\nif it is a profile model designed specifically for your custom user model.</p>\n<p>Setting it to <code class=\"docutils literal notranslate\">False</code> does not mean you can reference a swappable model\neven if it is swapped out - <code class=\"docutils literal notranslate\">False</code> just means that the migrations made\nwith this ForeignKey will always reference the exact model you specify\n(so it will fail hard if the user tries to run with a User model you don't\nsupport, for example).</p>\n<p>如果不确定，就保留它在默认为``True``的状态。</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"manytomanyfield\">\n<h3><code class=\"docutils literal notranslate\">ManyToManyField</code><a class=\"heading-anchor\" href=\"#manytomanyfield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ManyToManyField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">to</em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A many-to-many relationship. Requires a positional argument: the class to\nwhich the model is related, which works exactly the same as it does for\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>, including <a class=\"reference internal\" href=\"#recursive-relationships\"><span class=\"std std-ref\">recursive</span></a> and\n<a class=\"reference internal\" href=\"#lazy-relationships\"><span class=\"std std-ref\">lazy</span></a> relationships.</p>\n<p>通过这个字段的:class:~django.db.models.fields.related.RelatedManager，关联的对象可以被添加，删除，或者创建。</p>\n<section id=\"id1\">\n<h4>数据库表现<a class=\"heading-anchor\" href=\"#id1\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Behind the scenes, Django creates an intermediary join table to represent the\nmany-to-many relationship. By default, this table name is generated using the\nname of the many-to-many field and the name of the table for the model that\ncontains it. Since some databases don't support table names above a certain\nlength, these table names will be automatically truncated and a uniqueness hash\nwill be used, e.g. <code class=\"docutils literal notranslate\">author_books_9cdf</code>. You can manually provide the name of\nthe join table using the <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField.db_table\" title=\"django.db.models.ManyToManyField.db_table\"><code class=\"xref py py-attr docutils literal notranslate\">db_table</code></a> option.</p>\n</section>\n<section id=\"manytomany-arguments\">\n<span id=\"id2\"></span><h4>参数<a class=\"heading-anchor\" href=\"#manytomany-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> accepts an extra set of arguments -- all optional --\nthat control how the relationship functions.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.related_name\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">related_name</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.related_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Same as <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_name\" title=\"django.db.models.ForeignKey.related_name\"><code class=\"xref py py-attr docutils literal notranslate\">ForeignKey.related_name</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.related_query_name\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">related_query_name</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.related_query_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Same as <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_query_name\" title=\"django.db.models.ForeignKey.related_query_name\"><code class=\"xref py py-attr docutils literal notranslate\">ForeignKey.related_query_name</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.limit_choices_to\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">limit_choices_to</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.limit_choices_to\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Same as <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.limit_choices_to\" title=\"django.db.models.ForeignKey.limit_choices_to\"><code class=\"xref py py-attr docutils literal notranslate\">ForeignKey.limit_choices_to</code></a>.</p>\n<p><code class=\"docutils literal notranslate\">limit_choices_to</code> has no effect when used on a <code class=\"docutils literal notranslate\">ManyToManyField</code> with a\ncustom intermediate table specified using the\n<a class=\"reference internal\" href=\"#django.db.models.ManyToManyField.through\" title=\"django.db.models.ManyToManyField.through\"><code class=\"xref py py-attr docutils literal notranslate\">through</code></a> parameter.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.symmetrical\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">symmetrical</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.symmetrical\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>仅在定义自身为多对多字段关系时。考虑下面这个模型</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    friends <span class=\"o\">=</span> models<span class=\"o\">.</span>ManyToManyField<span class=\"p\">(</span><span class=\"s2\">&quot;self&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>当django处理这个模型时，它会做如此定义， 它对自身有一个:class:<a href=\"#id1\"><span class=\"problematic\" id=\"id2\">`</span></a>ManyToManyField`的关系，并且作为结果，它没有添加一个“person_set”属性到person这个类中。而是， 这个多对多字段关系被认为是对称的——即，如果我是你的朋友，那么你也是我的朋友。</p>\n<p>If you do not want symmetry in many-to-many relationships with <code class=\"docutils literal notranslate\">self</code>, set\n<a class=\"reference internal\" href=\"#django.db.models.ManyToManyField.symmetrical\" title=\"django.db.models.ManyToManyField.symmetrical\"><code class=\"xref py py-attr docutils literal notranslate\">symmetrical</code></a> to <code class=\"docutils literal notranslate\">False</code>. This will force Django to\nadd the descriptor for the reverse relationship, allowing\n<a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> relationships to be non-symmetrical.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.through\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">through</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.through\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Django will automatically generate a table to manage many-to-many\nrelationships. However, if you want to manually specify the intermediary\ntable, you can use the <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField.through\" title=\"django.db.models.ManyToManyField.through\"><code class=\"xref py py-attr docutils literal notranslate\">through</code></a> option to specify\nthe Django model that represents the intermediate table that you want to\nuse.</p>\n<p>The most common use for this option is when you want to associate\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#intermediary-manytomany\"><span class=\"std std-ref\">extra data with a many-to-many relationship</span></a>.</p>\n<p>If you don't specify an explicit <code class=\"docutils literal notranslate\">through</code> model, there is still an\nimplicit <code class=\"docutils literal notranslate\">through</code> model class you can use to directly access the table\ncreated to hold the association. It has three fields to link the models.</p>\n<p>如果源和目标模型不同，下面的字段会生成:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">id</code>: the primary key of the relation.</p></li>\n<li><p><code class=\"docutils literal notranslate\">&lt;containing_model&gt;_id</code>: the <code class=\"docutils literal notranslate\">id</code> of the model that declares the\n<code class=\"docutils literal notranslate\">ManyToManyField</code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">&lt;other_model&gt;_id</code>: the <code class=\"docutils literal notranslate\">id</code> of the model that the\n<code class=\"docutils literal notranslate\">ManyToManyField</code> points to.</p></li>\n</ul>\n<p>如果``ManyToManyField``指向的来源和目标是相同的模型， 下面的字段会生成：</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">id</code>: the primary key of the relation.</p></li>\n<li><p><code class=\"docutils literal notranslate\">from_&lt;model&gt;_id</code>: the <code class=\"docutils literal notranslate\">id</code> of the instance which points at the\nmodel (i.e. the source instance).</p></li>\n<li><p><code class=\"docutils literal notranslate\">to_&lt;model&gt;_id</code>: the <code class=\"docutils literal notranslate\">id</code> of the instance to which the relationship\npoints (i.e. the target model instance).</p></li>\n</ul>\n<p>这个类可以用来查询联接给出模型实例的记录，使用方式和普通模型类似。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.through_fields\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">through_fields</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.through_fields\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>仅在明确给出一个自定义中间模型时使用。django将会正常地的决定使用中间模型的那些字段来自动地建立一个多对多的关系。然而，考虑下面的模型。</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Person</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">50</span><span class=\"p\">)</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">Group</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">128</span><span class=\"p\">)</span>\n    members <span class=\"o\">=</span> models<span class=\"o\">.</span>ManyToManyField<span class=\"p\">(</span>\n        Person<span class=\"p\">,</span>\n        through<span class=\"o\">=</span><span class=\"s1\">&#39;Membership&#39;</span><span class=\"p\">,</span>\n        through_fields<span class=\"o\">=</span><span class=\"p\">(</span><span class=\"s1\">&#39;group&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;person&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">)</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">Membership</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    group <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Group<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    person <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>Person<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    inviter <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>\n        Person<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n        related_name<span class=\"o\">=</span><span class=\"s2\">&quot;membership_invites&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    invite_reason <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">64</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Membership 有*两个*外键``Person``（<code class=\"docutils literal notranslate\">person``和``inviter</code>），这使得联接关系显的歧义，django并不知道使用哪一个。这个例子中，你必须显式的指定Django应该使用哪个外键， 通过``through_fields``， 就想上例中一样。</p>\n<p><code class=\"docutils literal notranslate\">through_fields``接受一个2元组（``fiels1</code>，<code class=\"docutils literal notranslate\">fields2</code>）,其中``fields1``是多对多关系字段被定义的那个模型的外键名称（本例中的``group``）， <code class=\"docutils literal notranslate\">fields2``是目标模型的外键名称（本例中的``person</code>）。</p>\n<p>当你的中介表中有不止一个外键指向任一（或均）参与多对多关系的模型时， 你*必须*指定``through_fields``。这通用适用于``:ref:递归关系 &lt;recursive-relationships&gt; <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>中，场景是使用一个中介表并且不止两个外键指向同一个模型时，或者你需要显式地指定Django应该使用哪两个。</p>\n<p>Recursive relationships using an intermediary model are always defined as\nnon-symmetrical -- that is, with <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField.symmetrical\" title=\"django.db.models.ManyToManyField.symmetrical\"><code class=\"xref py py-attr docutils literal notranslate\">symmetrical=False</code></a>\n-- therefore, there is the concept of a &quot;source&quot; and a &quot;target&quot;. In that\ncase <code class=\"docutils literal notranslate\">'field1'</code> will be treated as the &quot;source&quot; of the relationship and\n<code class=\"docutils literal notranslate\">'field2'</code> as the &quot;target&quot;.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.db_table\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">db_table</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.db_table\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the table to create for storing the many-to-many data. If this\nis not provided, Django will assume a default name based upon the names of:\nthe table for the model defining the relationship and the name of the field\nitself.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.db_constraint\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">db_constraint</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.db_constraint\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>控制是否中介模型中外键的约束应该在数据库中被创建。默认值是True， 而且这几乎确定就是你需要的功能。将这个设为False会损坏数据的完整性。即便如此，一下场景一可能需要这样做：</p>\n<ul class=\"simple\">\n<li><p>你有无效的冗余数据</p></li>\n<li><p>你正在共享你的数据库</p></li>\n</ul>\n<p>同时传递&quot;db_constraint&quot;和“through”会引发错误。</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.ManyToManyField.swappable\">\n<span class=\"sig-prename descclassname\">ManyToManyField.</span><span class=\"sig-name descname\">swappable</span><a class=\"heading-anchor\" href=\"#django.db.models.ManyToManyField.swappable\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Controls the migration framework's reaction if this <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a>\nis pointing at a swappable model. If it is <code class=\"docutils literal notranslate\">True</code> - the default -\nthen if the <a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> is pointing at a model which matches\nthe current value of <code class=\"docutils literal notranslate\">settings.AUTH_USER_MODEL</code> (or another swappable\nmodel setting) the relationship will be stored in the migration using\na reference to the setting, not to the model directly.</p>\n<p>You only want to override this to be <code class=\"docutils literal notranslate\">False</code> if you are sure your\nmodel should always point towards the swapped-in model - for example,\nif it is a profile model designed specifically for your custom user model.</p>\n<p>如果不确定，就保留它在默认为``True``的状态。</p>\n</dd></dl>\n\n<p><a class=\"reference internal\" href=\"#django.db.models.ManyToManyField\" title=\"django.db.models.ManyToManyField\"><code class=\"xref py py-class docutils literal notranslate\">ManyToManyField</code></a> does not support <a class=\"reference internal\" href=\"#django.db.models.Field.validators\" title=\"django.db.models.Field.validators\"><code class=\"xref py py-attr docutils literal notranslate\">validators</code></a>.</p>\n<p><a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a> has no effect since there is no way to require a\nrelationship at the database level.</p>\n</section>\n</section>\n<section id=\"onetoonefield\">\n<h3><code class=\"docutils literal notranslate\">OneToOneField</code><a class=\"heading-anchor\" href=\"#onetoonefield\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.OneToOneField\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">OneToOneField</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">to</em>, <em class=\"sig-param\">on_delete</em>, <em class=\"sig-param\">parent_link<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>options</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.OneToOneField\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A one-to-one relationship. Conceptually, this is similar to a\n<a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> with <a class=\"reference internal\" href=\"#django.db.models.Field.unique\" title=\"django.db.models.Field.unique\"><code class=\"xref py py-attr docutils literal notranslate\">unique=True</code></a>, but the\n&quot;reverse&quot; side of the relation will directly return a single object.</p>\n<p>This is most useful as the primary key of a model which &quot;extends&quot;\nanother model in some way; <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/#multi-table-inheritance\"><span class=\"std std-ref\">多表继承</span></a> is\nimplemented by adding an implicit one-to-one relation from the child\nmodel to the parent model, for example.</p>\n<p>One positional argument is required: the class to which the model will be\nrelated. This works exactly the same as it does for <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>,\nincluding all the options regarding <a class=\"reference internal\" href=\"#recursive-relationships\"><span class=\"std std-ref\">recursive</span></a>\nand <a class=\"reference internal\" href=\"#lazy-relationships\"><span class=\"std std-ref\">lazy</span></a> relationships.</p>\n<p>If you do not specify the <a class=\"reference internal\" href=\"#django.db.models.ForeignKey.related_name\" title=\"django.db.models.ForeignKey.related_name\"><code class=\"xref py py-attr docutils literal notranslate\">related_name</code></a> argument for the\n<code class=\"docutils literal notranslate\">OneToOneField</code>, Django will use the lowercase name of the current model as\ndefault value.</p>\n<p>With the following example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">MySpecialUser</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    user <span class=\"o\">=</span> models<span class=\"o\">.</span>OneToOneField<span class=\"p\">(</span>\n        settings<span class=\"o\">.</span>AUTH_USER_MODEL<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    supervisor <span class=\"o\">=</span> models<span class=\"o\">.</span>OneToOneField<span class=\"p\">(</span>\n        settings<span class=\"o\">.</span>AUTH_USER_MODEL<span class=\"p\">,</span>\n        on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">,</span>\n        related_name<span class=\"o\">=</span><span class=\"s1\">&#39;supervisor_of&#39;</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n<p>your resulting <code class=\"docutils literal notranslate\">User</code> model will have the following attributes:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>user <span class=\"o\">=</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>pk<span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">hasattr</span><span class=\"p\">(</span>user<span class=\"p\">,</span> <span class=\"s1\">&#39;myspecialuser&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">True</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">hasattr</span><span class=\"p\">(</span>user<span class=\"p\">,</span> <span class=\"s1\">&#39;supervisor_of&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">True</span>\n</code></pre></div>\n<p>A <code class=\"docutils literal notranslate\">DoesNotExist</code> exception is raised when accessing the reverse relationship\nif an entry in the related table doesn't exist. For example, if a user doesn't\nhave a supervisor designated by <code class=\"docutils literal notranslate\">MySpecialUser</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>user<span class=\"o\">.</span>supervisor_of\n<span class=\"gt\">Traceback (most recent call last):</span>\n    <span class=\"o\">...</span>\n<span class=\"gr\">DoesNotExist</span>: User matching query does not exist.\n</code></pre></div>\n<p id=\"onetoone-arguments\">Additionally, <code class=\"docutils literal notranslate\">OneToOneField</code> accepts all of the extra arguments\naccepted by <a class=\"reference internal\" href=\"#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>, plus one extra argument:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.OneToOneField.parent_link\">\n<span class=\"sig-prename descclassname\">OneToOneField.</span><span class=\"sig-name descname\">parent_link</span><a class=\"heading-anchor\" href=\"#django.db.models.OneToOneField.parent_link\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>When <code class=\"docutils literal notranslate\">True</code> and used in a model which inherits from another\n<a class=\"reference internal\" href=\"/zh-hans/2.2/glossary/#term-concrete-model\"><span class=\"xref std std-term\">concrete model</span></a>, indicates that this field should be used as the\nlink back to the parent class, rather than the extra\n<code class=\"docutils literal notranslate\">OneToOneField</code> which would normally be implicitly created by\nsubclassing.</p>\n</dd></dl>\n\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/examples/one_to_one/\"><span class=\"doc\">One-to-one relationships</span></a> for usage\nexamples of <code class=\"docutils literal notranslate\">OneToOneField</code>.</p>\n</section>\n</section>\n<section id=\"field-api-reference\">\n<h2>Field API reference<a class=\"heading-anchor\" href=\"#field-api-reference\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Field</span><a class=\"heading-anchor\" href=\"#django.db.models.Field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\">Field</code> is an abstract class that represents a database table column.\nDjango uses fields to create the database table (<a class=\"reference internal\" href=\"#django.db.models.Field.db_type\" title=\"django.db.models.Field.db_type\"><code class=\"xref py py-meth docutils literal notranslate\">db_type()</code></a>), to map\nPython types to database (<a class=\"reference internal\" href=\"#django.db.models.Field.get_prep_value\" title=\"django.db.models.Field.get_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_prep_value()</code></a>) and vice-versa\n(<a class=\"reference internal\" href=\"#django.db.models.Field.from_db_value\" title=\"django.db.models.Field.from_db_value\"><code class=\"xref py py-meth docutils literal notranslate\">from_db_value()</code></a>).</p>\n<p>A field is thus a fundamental piece in different Django APIs, notably,\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\">models</code></a> and <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\">querysets</code></a>.</p>\n<p>In models, a field is instantiated as a class attribute and represents a\nparticular table column, see <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/db/models/\"><span class=\"doc\">模型</span></a>. It has attributes\nsuch as <a class=\"reference internal\" href=\"#django.db.models.Field.null\" title=\"django.db.models.Field.null\"><code class=\"xref py py-attr docutils literal notranslate\">null</code></a> and <a class=\"reference internal\" href=\"#django.db.models.Field.unique\" title=\"django.db.models.Field.unique\"><code class=\"xref py py-attr docutils literal notranslate\">unique</code></a>, and methods that Django uses to\nmap the field value to database-specific values.</p>\n<p>A <code class=\"docutils literal notranslate\">Field</code> is a subclass of\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/lookups/#django.db.models.lookups.RegisterLookupMixin\" title=\"django.db.models.lookups.RegisterLookupMixin\"><code class=\"xref py py-class docutils literal notranslate\">RegisterLookupMixin</code></a> and thus both\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/lookups/#django.db.models.Transform\" title=\"django.db.models.Transform\"><code class=\"xref py py-class docutils literal notranslate\">Transform</code></a> and\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/lookups/#django.db.models.Lookup\" title=\"django.db.models.Lookup\"><code class=\"xref py py-class docutils literal notranslate\">Lookup</code></a> can be registered on it to be used\nin <code class=\"docutils literal notranslate\">QuerySet</code>s (e.g. <code class=\"docutils literal notranslate\">field_name__exact=&quot;foo&quot;</code>). All <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/querysets/#field-lookups\"><span class=\"std std-ref\">built-in\nlookups</span></a> are registered by default.</p>\n<p>All of Django's built-in fields, such as <a class=\"reference internal\" href=\"#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a>, are particular\nimplementations of <code class=\"docutils literal notranslate\">Field</code>. If you need a custom field, you can either\nsubclass any of the built-in fields or write a <code class=\"docutils literal notranslate\">Field</code> from scratch. In\neither case, see <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/\"><span class=\"doc\">编写自定义模型字段(model fields)</span></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.description\">\n<span class=\"sig-name descname\">description</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.description\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A verbose description of the field, e.g. for the\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/contrib/admin/admindocs/#module-django.contrib.admindocs\" title=\"django.contrib.admindocs: Django's admin documentation generator.\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.admindocs</code></a> application.</p>\n<p>The description can be of the form:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>description <span class=\"o\">=</span> _<span class=\"p\">(</span><span class=\"s2\">&quot;String (up to </span><span class=\"si\">%(max_length)s</span><span class=\"s2\">)&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>where the arguments are interpolated from the field's <code class=\"docutils literal notranslate\">__dict__</code>.</p>\n</dd></dl>\n\n<p>To map a <code class=\"docutils literal notranslate\">Field</code> to a database-specific type, Django exposes several\nmethods:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.get_internal_type\">\n<span class=\"sig-name descname\">get_internal_type</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.get_internal_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a string naming this field for backend specific purposes.\nBy default, it returns the class name.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#emulating-built-in-field-types\"><span class=\"std std-ref\">仿造内置字段类型</span></a> for usage in custom fields.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.db_type\">\n<span class=\"sig-name descname\">db_type</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">connection</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.db_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the database column data type for the <a class=\"reference internal\" href=\"#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a>, taking\ninto account the <code class=\"docutils literal notranslate\">connection</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#custom-database-types\"><span class=\"std std-ref\">自定义数据库类型</span></a> for usage in custom fields.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.rel_db_type\">\n<span class=\"sig-name descname\">rel_db_type</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">connection</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.rel_db_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the database column data type for fields such as <code class=\"docutils literal notranslate\">ForeignKey</code>\nand <code class=\"docutils literal notranslate\">OneToOneField</code> that point to the <a class=\"reference internal\" href=\"#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a>, taking\ninto account the <code class=\"docutils literal notranslate\">connection</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#custom-database-types\"><span class=\"std std-ref\">自定义数据库类型</span></a> for usage in custom fields.</p>\n</dd></dl>\n\n<p>There are three main situations where Django needs to interact with the\ndatabase backend and fields:</p>\n<ul class=\"simple\">\n<li><p>when it queries the database (Python value -&gt; database backend value)</p></li>\n<li><p>when it loads data from the database (database backend value -&gt; Python\nvalue)</p></li>\n<li><p>when it saves to the database (Python value -&gt; database backend value)</p></li>\n</ul>\n<p>When querying, <a class=\"reference internal\" href=\"#django.db.models.Field.get_db_prep_value\" title=\"django.db.models.Field.get_db_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_db_prep_value()</code></a> and <a class=\"reference internal\" href=\"#django.db.models.Field.get_prep_value\" title=\"django.db.models.Field.get_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_prep_value()</code></a> are used:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.get_prep_value\">\n<span class=\"sig-name descname\">get_prep_value</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">value</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.get_prep_value\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p><code class=\"docutils literal notranslate\">value</code> is the current value of the model's attribute, and the method\nshould return data in a format that has been prepared for use as a\nparameter in a query.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#converting-python-objects-to-query-values\"><span class=\"std std-ref\">将 Python 转为查询值</span></a> for usage.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.get_db_prep_value\">\n<span class=\"sig-name descname\">get_db_prep_value</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">value</em>, <em class=\"sig-param\">connection</em>, <em class=\"sig-param\">prepared<span class=\"o\">=</span><span class=\"default_value\">False</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.get_db_prep_value\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Converts <code class=\"docutils literal notranslate\">value</code> to a backend-specific value. By default it returns\n<code class=\"docutils literal notranslate\">value</code> if <code class=\"docutils literal notranslate\">prepared=True</code> and <a class=\"reference internal\" href=\"#django.db.models.Field.get_prep_value\" title=\"django.db.models.Field.get_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_prep_value()</code></a> if is\n<code class=\"docutils literal notranslate\">False</code>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#converting-query-values-to-database-values\"><span class=\"std std-ref\">将查询值转为数据库值</span></a> for usage.</p>\n</dd></dl>\n\n<p>When loading data, <a class=\"reference internal\" href=\"#django.db.models.Field.from_db_value\" title=\"django.db.models.Field.from_db_value\"><code class=\"xref py py-meth docutils literal notranslate\">from_db_value()</code></a> is used:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.from_db_value\">\n<span class=\"sig-name descname\">from_db_value</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">value</em>, <em class=\"sig-param\">expression</em>, <em class=\"sig-param\">connection</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.from_db_value\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Converts a value as returned by the database to a Python object. It is\nthe reverse of <a class=\"reference internal\" href=\"#django.db.models.Field.get_prep_value\" title=\"django.db.models.Field.get_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_prep_value()</code></a>.</p>\n<p>This method is not used for most built-in fields as the database\nbackend already returns the correct Python type, or the backend itself\ndoes the conversion.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#converting-values-to-python-objects\"><span class=\"std std-ref\">将值转为 Python 对象</span></a> for usage.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>For performance reasons, <code class=\"docutils literal notranslate\">from_db_value</code> is not implemented as a\nno-op on fields which do not require it (all Django fields).\nConsequently you may not call <code class=\"docutils literal notranslate\">super</code> in your definition.</p>\n</aside>\n</dd></dl>\n\n<p>When saving, <a class=\"reference internal\" href=\"#django.db.models.Field.pre_save\" title=\"django.db.models.Field.pre_save\"><code class=\"xref py py-meth docutils literal notranslate\">pre_save()</code></a> and <a class=\"reference internal\" href=\"#django.db.models.Field.get_db_prep_save\" title=\"django.db.models.Field.get_db_prep_save\"><code class=\"xref py py-meth docutils literal notranslate\">get_db_prep_save()</code></a> are used:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.get_db_prep_save\">\n<span class=\"sig-name descname\">get_db_prep_save</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">value</em>, <em class=\"sig-param\">connection</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.get_db_prep_save\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Same as the <a class=\"reference internal\" href=\"#django.db.models.Field.get_db_prep_value\" title=\"django.db.models.Field.get_db_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_db_prep_value()</code></a>, but called when the field value\nmust be <em>saved</em> to the database. By default returns\n<a class=\"reference internal\" href=\"#django.db.models.Field.get_db_prep_value\" title=\"django.db.models.Field.get_db_prep_value\"><code class=\"xref py py-meth docutils literal notranslate\">get_db_prep_value()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.pre_save\">\n<span class=\"sig-name descname\">pre_save</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model_instance</em>, <em class=\"sig-param\">add</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.pre_save\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Method called prior to <a class=\"reference internal\" href=\"#django.db.models.Field.get_db_prep_save\" title=\"django.db.models.Field.get_db_prep_save\"><code class=\"xref py py-meth docutils literal notranslate\">get_db_prep_save()</code></a> to prepare the value\nbefore being saved (e.g. for <a class=\"reference internal\" href=\"#django.db.models.DateField.auto_now\" title=\"django.db.models.DateField.auto_now\"><code class=\"xref py py-attr docutils literal notranslate\">DateField.auto_now</code></a>).</p>\n<p><code class=\"docutils literal notranslate\">model_instance</code> is the instance this field belongs to and <code class=\"docutils literal notranslate\">add</code>\nis whether the instance is being saved to the database for the first\ntime.</p>\n<p>It should return the value of the appropriate attribute from\n<code class=\"docutils literal notranslate\">model_instance</code> for this field. The attribute name is in\n<code class=\"docutils literal notranslate\">self.attname</code> (this is set up by <a class=\"reference internal\" href=\"#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\">Field</code></a>).</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#preprocessing-values-before-saving\"><span class=\"std std-ref\">在保存前预处理数值</span></a> for usage.</p>\n</dd></dl>\n\n<p>Fields often receive their values as a different type, either from\nserialization or from forms.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.to_python\">\n<span class=\"sig-name descname\">to_python</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">value</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.to_python\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Converts the value into the correct Python object. It acts as the\nreverse of <a class=\"reference internal\" href=\"#django.db.models.Field.value_to_string\" title=\"django.db.models.Field.value_to_string\"><code class=\"xref py py-meth docutils literal notranslate\">value_to_string()</code></a>, and is also called in\n<a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/instances/#django.db.models.Model.clean\" title=\"django.db.models.Model.clean\"><code class=\"xref py py-meth docutils literal notranslate\">clean()</code></a>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#converting-values-to-python-objects\"><span class=\"std std-ref\">将值转为 Python 对象</span></a> for usage.</p>\n</dd></dl>\n\n<p>Besides saving to the database, the field also needs to know how to\nserialize its value:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.value_from_object\">\n<span class=\"sig-name descname\">value_from_object</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">obj</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.value_from_object\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the field's value for the given model instance.</p>\n<p>This method is often used by <a class=\"reference internal\" href=\"#django.db.models.Field.value_to_string\" title=\"django.db.models.Field.value_to_string\"><code class=\"xref py py-meth docutils literal notranslate\">value_to_string()</code></a>.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.value_to_string\">\n<span class=\"sig-name descname\">value_to_string</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">obj</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.value_to_string\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Converts <code class=\"docutils literal notranslate\">obj</code> to a string. Used to serialize the value of the field.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#converting-model-field-to-serialization\"><span class=\"std std-ref\">为序列化转换字段数据</span></a> for usage.</p>\n</dd></dl>\n\n<p>When using <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">model forms</code></a>, the <code class=\"docutils literal notranslate\">Field</code>\nneeds to know which form field it should be represented by:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.formfield\">\n<span class=\"sig-name descname\">formfield</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">form_class<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">choices_form_class<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.formfield\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the default <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.Field\" title=\"django.forms.Field\"><code class=\"xref py py-class docutils literal notranslate\">django.forms.Field</code></a> of this field for\n<a class=\"reference internal\" href=\"/zh-hans/2.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>.</p>\n<p>By default, if both <code class=\"docutils literal notranslate\">form_class</code> and <code class=\"docutils literal notranslate\">choices_form_class</code> are\n<code class=\"docutils literal notranslate\">None</code>, it uses <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.CharField\" title=\"django.forms.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a>. If the field has\n<a class=\"reference internal\" href=\"#django.db.models.Field.choices\" title=\"django.db.models.Field.choices\"><code class=\"xref py py-attr docutils literal notranslate\">choices</code></a> and <code class=\"docutils literal notranslate\">choices_form_class</code>\nisn't specified, it uses <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/forms/fields/#django.forms.TypedChoiceField\" title=\"django.forms.TypedChoiceField\"><code class=\"xref py py-class docutils literal notranslate\">TypedChoiceField</code></a>.</p>\n<p>See <a class=\"reference internal\" href=\"/zh-hans/2.2/howto/custom-model-fields/#specifying-form-field-for-model-field\"><span class=\"std std-ref\">为模型字段指定表单字段</span></a> for usage.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.deconstruct\">\n<span class=\"sig-name descname\">deconstruct</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.deconstruct\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a 4-tuple with enough information to recreate the field:</p>\n<ol class=\"arabic simple\">\n<li><p>The name of the field on the model.</p></li>\n<li><p>The import path of the field (e.g. <code class=\"docutils literal notranslate\">&quot;django.db.models.IntegerField&quot;</code>).\nThis should be the most portable version, so less specific may be better.</p></li>\n<li><p>A list of positional arguments.</p></li>\n<li><p>A dict of keyword arguments.</p></li>\n</ol>\n<p>This method must be added to fields prior to 1.7 to migrate its data\nusing <a class=\"reference internal\" href=\"/zh-hans/2.2/topics/migrations/\"><span class=\"doc\">迁移</span></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n</section>\n</section>\n<section id=\"field-attribute-reference\">\n<span id=\"model-field-attributes\"></span><h1>Field attribute reference<a class=\"heading-anchor\" href=\"#field-attribute-reference\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Every <code class=\"docutils literal notranslate\">Field</code> instance contains several attributes that allow\nintrospecting its behavior. Use these attributes instead of <code class=\"docutils literal notranslate\">isinstance</code>\nchecks when you need to write code that depends on a field's functionality.\nThese attributes can be used together with the <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/meta/#model-meta-field-api\"><span class=\"std std-ref\">Model._meta API</span></a> to narrow down a search for specific field types.\nCustom model fields should implement these flags.</p>\n<section id=\"attributes-for-fields\">\n<h2>Attributes for fields<a class=\"heading-anchor\" href=\"#attributes-for-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.auto_created\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">auto_created</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.auto_created\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that indicates if the field was automatically created, such\nas the <code class=\"docutils literal notranslate\">OneToOneField</code> used by model inheritance.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.concrete\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">concrete</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.concrete\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that indicates if the field has a database column associated\nwith it.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.hidden\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">hidden</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.hidden\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that indicates if a field is used to back another non-hidden\nfield's functionality (e.g. the <code class=\"docutils literal notranslate\">content_type</code> and <code class=\"docutils literal notranslate\">object_id</code> fields\nthat make up a <code class=\"docutils literal notranslate\">GenericForeignKey</code>). The <code class=\"docutils literal notranslate\">hidden</code> flag is used to\ndistinguish what constitutes the public subset of fields on the model from\nall the fields on the model.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/2.2/ref/models/meta/#django.db.models.options.Options.get_fields\" title=\"django.db.models.options.Options.get_fields\"><code class=\"xref py py-meth docutils literal notranslate\">Options.get_fields()</code></a>\nexcludes hidden fields by default. Pass in <code class=\"docutils literal notranslate\">include_hidden=True</code> to\nreturn hidden fields in the results.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.is_relation\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">is_relation</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.is_relation\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that indicates if a field contains references to one or\nmore other models for its functionality (e.g. <code class=\"docutils literal notranslate\">ForeignKey</code>,\n<code class=\"docutils literal notranslate\">ManyToManyField</code>, <code class=\"docutils literal notranslate\">OneToOneField</code>, etc.).</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.model\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">model</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the model on which the field is defined. If a field is defined on\na superclass of a model, <code class=\"docutils literal notranslate\">model</code> will refer to the superclass, not the\nclass of the instance.</p>\n</dd></dl>\n\n</section>\n<section id=\"attributes-for-fields-with-relations\">\n<h2>Attributes for fields with relations<a class=\"heading-anchor\" href=\"#attributes-for-fields-with-relations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>These attributes are used to query for the cardinality and other details of a\nrelation. These attribute are present on all fields; however, they will only\nhave boolean values (rather than <code class=\"docutils literal notranslate\">None</code>) if the field is a relation type\n(<a class=\"reference internal\" href=\"#django.db.models.Field.is_relation\" title=\"django.db.models.Field.is_relation\"><code class=\"xref py py-attr docutils literal notranslate\">Field.is_relation=True</code></a>).</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.many_to_many\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">many_to_many</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.many_to_many\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that is <code class=\"docutils literal notranslate\">True</code> if the field has a many-to-many relation;\n<code class=\"docutils literal notranslate\">False</code> otherwise. The only field included with Django where this is\n<code class=\"docutils literal notranslate\">True</code> is <code class=\"docutils literal notranslate\">ManyToManyField</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.many_to_one\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">many_to_one</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.many_to_one\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that is <code class=\"docutils literal notranslate\">True</code> if the field has a many-to-one relation, such\nas a <code class=\"docutils literal notranslate\">ForeignKey</code>; <code class=\"docutils literal notranslate\">False</code> otherwise.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.one_to_many\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">one_to_many</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.one_to_many\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that is <code class=\"docutils literal notranslate\">True</code> if the field has a one-to-many relation, such\nas a <code class=\"docutils literal notranslate\">GenericRelation</code> or the reverse of a <code class=\"docutils literal notranslate\">ForeignKey</code>; <code class=\"docutils literal notranslate\">False</code>\notherwise.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.one_to_one\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">one_to_one</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.one_to_one\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Boolean flag that is <code class=\"docutils literal notranslate\">True</code> if the field has a one-to-one relation, such\nas a <code class=\"docutils literal notranslate\">OneToOneField</code>; <code class=\"docutils literal notranslate\">False</code> otherwise.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Field.related_model\">\n<span class=\"sig-prename descclassname\">Field.</span><span class=\"sig-name descname\">related_model</span><a class=\"heading-anchor\" href=\"#django.db.models.Field.related_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Points to the model the field relates to. For example, <code class=\"docutils literal notranslate\">Author</code> in\n<code class=\"docutils literal notranslate\">ForeignKey(Author, on_delete=models.CASCADE)</code>. The <code class=\"docutils literal notranslate\">related_model</code> for\na <code class=\"docutils literal notranslate\">GenericForeignKey</code> is always <code class=\"docutils literal notranslate\">None</code>.</p>\n</dd></dl>\n\n</section>\n</section>\n","rootId":"","toc":[{"title":"模型字段参考","anchor":"","children":[{"title":"字段选项","anchor":"field-options","children":[{"title":"null","anchor":"null","children":[]},{"title":"blank","anchor":"blank","children":[]},{"title":"choices","anchor":"choices","children":[]},{"title":"db_column","anchor":"db-column","children":[]},{"title":"db_index","anchor":"db-index","children":[]},{"title":"db_tablespace","anchor":"db-tablespace","children":[]},{"title":"default","anchor":"default","children":[]},{"title":"editable","anchor":"editable","children":[]},{"title":"error_messages","anchor":"error-messages","children":[]},{"title":"help_text","anchor":"help-text","children":[]},{"title":"primary_key","anchor":"primary-key","children":[]},{"title":"unique","anchor":"unique","children":[]},{"title":"unique_for_date","anchor":"unique-for-date","children":[]},{"title":"unique_for_month","anchor":"unique-for-month","children":[]},{"title":"unique_for_year","anchor":"unique-for-year","children":[]},{"title":"verbose_name","anchor":"verbose-name","children":[]},{"title":"validators","anchor":"validators","children":[{"title":"Registering and fetching lookups","anchor":"registering-and-fetching-lookups","children":[]}]}]},{"title":"字段类型","anchor":"field-types","children":[{"title":"AutoField","anchor":"autofield","children":[]},{"title":"BigAutoField","anchor":"bigautofield","children":[]},{"title":"BigIntegerField","anchor":"bigintegerfield","children":[]},{"title":"BinaryField","anchor":"binaryfield","children":[]},{"title":"BooleanField","anchor":"booleanfield","children":[]},{"title":"CharField","anchor":"charfield","children":[]},{"title":"DateField","anchor":"datefield","children":[]},{"title":"DateTimeField","anchor":"datetimefield","children":[]},{"title":"DecimalField","anchor":"decimalfield","children":[]},{"title":"DurationField","anchor":"durationfield","children":[]},{"title":"EmailField","anchor":"emailfield","children":[]},{"title":"FileField","anchor":"filefield","children":[{"title":"FileField``和``FieldFile","anchor":"filefield-and-fieldfile","children":[]}]},{"title":"FilePathField","anchor":"filepathfield","children":[]},{"title":"FloatField","anchor":"floatfield","children":[]},{"title":"ImageField","anchor":"imagefield","children":[]},{"title":"IntegerField","anchor":"integerfield","children":[]},{"title":"GenericIPAddressField","anchor":"genericipaddressfield","children":[]},{"title":"NullBooleanField","anchor":"nullbooleanfield","children":[]},{"title":"PositiveIntegerField","anchor":"positiveintegerfield","children":[]},{"title":"PositiveSmallIntegerField","anchor":"positivesmallintegerfield","children":[]},{"title":"SlugField","anchor":"slugfield","children":[]},{"title":"SmallIntegerField","anchor":"smallintegerfield","children":[]},{"title":"TextField","anchor":"textfield","children":[]},{"title":"TimeField","anchor":"timefield","children":[]},{"title":"URLField","anchor":"urlfield","children":[]},{"title":"UUIDField","anchor":"uuidfield","children":[]}]},{"title":"关联关系字段","anchor":"module-django.db.models.fields.related","children":[{"title":"ForeignKey","anchor":"foreignkey","children":[{"title":"数据库表现","anchor":"database-representation","children":[]},{"title":"参数","anchor":"arguments","children":[]}]},{"title":"ManyToManyField","anchor":"manytomanyfield","children":[{"title":"数据库表现","anchor":"id1","children":[]},{"title":"参数","anchor":"manytomany-arguments","children":[]}]},{"title":"OneToOneField","anchor":"onetoonefield","children":[]}]},{"title":"Field API reference","anchor":"field-api-reference","children":[]}]},{"title":"Field attribute reference","anchor":"field-attribute-reference","children":[{"title":"Attributes for fields","anchor":"attributes-for-fields","children":[]},{"title":"Attributes for fields with relations","anchor":"attributes-for-fields-with-relations","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API参考","url":"/zh-hans/2.2/ref/"},{"docname":"ref/models/index","title":"模型","url":"/zh-hans/2.2/ref/models/"}],"prev":{"docname":"ref/models/index","title":"模型","url":"/zh-hans/2.2/ref/models/"},"next":{"docname":"ref/models/indexes","title":"Model index reference","url":"/zh-hans/2.2/ref/models/indexes/"},"formats":{"html":"/zh-hans/2.2/ref/models/fields/","markdown":"/zh-hans/2.2/ref/models/fields.md","json":"/zh-hans/2.2/ref/models/fields.json"},"source":"https://github.com/django/django/blob/stable/2.2.x/docs/ref/models/fields.txt","official":"https://docs.djangoproject.com/zh-hans/2.2/ref/models/fields/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}