Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 775

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Model field reference &#8212; Django 1.8.19 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.8.19',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../../contents.html" />
    <link rel="up" title="Models" href="index.html" />
    <link rel="next" title="Model _meta API" href="meta.html" />
    <link rel="prev" title="Models" href="index.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="Models">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="meta.html" title="Model &lt;code class=&#34;docutils literal&#34;&gt;&lt;span class=&#34;pre&#34;&gt;_meta&lt;/span&gt;&lt;/code&gt; API">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-fields">
            
  <div class="section" id="s-module-django.db.models.fields">
<span id="s-model-field-reference"></span><span id="module-django.db.models.fields"></span><span id="model-field-reference"></span><h1>Model field reference<a class="headerlink" href="#module-django.db.models.fields" title="Permalink to this headline">¶</a></h1>
<p>This document contains all the API references of <a class="reference internal" href="#django.db.models.Field" title="django.db.models.Field"><code class="xref py py-class docutils literal"><span class="pre">Field</span></code></a> including the
<a class="reference internal" href="#field-options">field options</a> and <a class="reference internal" href="#field-types">field types</a> Django offers.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">If the built-in fields don&#8217;t do the trick, you can try <a class="reference external" href="https://django-localflavor.readthedocs.org/">django-localflavor</a>, which contains assorted
pieces of code that are useful for particular countries or cultures. Also,
you can easily <a class="reference internal" href="../../howto/custom-model-fields.html"><span class="doc">write your own custom model fields</span></a>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Technically, these models are defined in <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"><span class="pre">django.db.models.fields</span></code></a>, but
for convenience they&#8217;re imported into <a class="reference internal" href="../../topics/db/models.html#module-django.db.models" title="django.db.models"><code class="xref py py-mod docutils literal"><span class="pre">django.db.models</span></code></a>; the standard
convention is to use <code class="docutils literal"><span class="pre">from</span> <span class="pre">django.db</span> <span class="pre">import</span> <span class="pre">models</span></code> and refer to fields as
<code class="docutils literal"><span class="pre">models.&lt;Foo&gt;Field</span></code>.</p>
</div>
<div class="section" id="s-field-options">
<span id="s-common-model-field-options"></span><span id="field-options"></span><span id="common-model-field-options"></span><h2>Field options<a class="headerlink" href="#field-options" title="Permalink to this headline">¶</a></h2>
<p>The following arguments are available to all field types. All are optional.</p>
<div class="section" id="s-null">
<span id="null"></span><h3><code class="docutils literal"><span class="pre">null</span></code><a class="headerlink" href="#null" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.null">
<code class="descclassname">Field.</code><code class="descname">null</code><a class="headerlink" href="#django.db.models.Field.null" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">True</span></code>, Django will store empty values as <code class="docutils literal"><span class="pre">NULL</span></code> in the database. Default
is <code class="docutils literal"><span class="pre">False</span></code>.</p>
<p>Avoid using <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><code class="xref py py-attr docutils literal"><span class="pre">null</span></code></a> on string-based 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"><span class="pre">CharField</span></code></a> and <a class="reference internal" href="#django.db.models.TextField" title="django.db.models.TextField"><code class="xref py py-class docutils literal"><span class="pre">TextField</span></code></a> because empty string values will
always be stored as empty strings, not as <code class="docutils literal"><span class="pre">NULL</span></code>. If a string-based field has
<code class="docutils literal"><span class="pre">null=True</span></code>, that means it has two possible values for &#8220;no data&#8221;: <code class="docutils literal"><span class="pre">NULL</span></code>,
and the empty string. In most cases, it&#8217;s redundant to have two possible values
for &#8220;no data;&#8221; the Django convention is to use the empty string, not <code class="docutils literal"><span class="pre">NULL</span></code>.</p>
<p>For both string-based and non-string-based fields, you will also need to
set <code class="docutils literal"><span class="pre">blank=True</span></code> if you wish to permit empty values in forms, as the
<a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><code class="xref py py-attr docutils literal"><span class="pre">null</span></code></a> parameter only affects database storage
(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"><span class="pre">blank</span></code></a>).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When using the Oracle database backend, the value <code class="docutils literal"><span class="pre">NULL</span></code> will be stored to
denote the empty string regardless of this attribute.</p>
</div>
<p>If you want to accept <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><code class="xref py py-attr docutils literal"><span class="pre">null</span></code></a> values with <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><code class="xref py py-class docutils literal"><span class="pre">BooleanField</span></code></a>,
use <a class="reference internal" href="#django.db.models.NullBooleanField" title="django.db.models.NullBooleanField"><code class="xref py py-class docutils literal"><span class="pre">NullBooleanField</span></code></a> instead.</p>
</div>
<div class="section" id="s-blank">
<span id="blank"></span><h3><code class="docutils literal"><span class="pre">blank</span></code><a class="headerlink" href="#blank" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.blank">
<code class="descclassname">Field.</code><code class="descname">blank</code><a class="headerlink" href="#django.db.models.Field.blank" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">True</span></code>, the field is allowed to be blank. Default is <code class="docutils literal"><span class="pre">False</span></code>.</p>
<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"><span class="pre">null</span></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"><span class="pre">null</span></code></a> is
purely 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"><span class="pre">blank</span></code></a> is validation-related. If
a field has <code class="docutils literal"><span class="pre">blank=True</span></code>, form validation will allow entry of an empty value.
If a field has <code class="docutils literal"><span class="pre">blank=False</span></code>, the field will be required.</p>
</div>
<div class="section" id="s-choices">
<span id="s-field-choices"></span><span id="choices"></span><span id="field-choices"></span><h3><code class="docutils literal"><span class="pre">choices</span></code><a class="headerlink" href="#choices" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.choices">
<code class="descclassname">Field.</code><code class="descname">choices</code><a class="headerlink" href="#django.db.models.Field.choices" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An iterable (e.g., a list or tuple) consisting itself of iterables of exactly
two items (e.g. <code class="docutils literal"><span class="pre">[(A,</span> <span class="pre">B),</span> <span class="pre">(A,</span> <span class="pre">B)</span> <span class="pre">...]</span></code>) to use as choices for this field. If
this is given, the default form widget will be a select box with these choices
instead of the standard text field.</p>
<p>The first element in each tuple is the actual value to be set on the model,
and the second element is the human-readable name. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <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>
    <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>
    <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>
    <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>
<span class="p">)</span>
</pre></div>
</div>
<p>Generally, it&#8217;s best to define choices inside a model class, and to
define a suitably-named constant for each value:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">FRESHMAN</span> <span class="o">=</span> <span class="s1">&#39;FR&#39;</span>
    <span class="n">SOPHOMORE</span> <span class="o">=</span> <span class="s1">&#39;SO&#39;</span>
    <span class="n">JUNIOR</span> <span class="o">=</span> <span class="s1">&#39;JR&#39;</span>
    <span class="n">SENIOR</span> <span class="o">=</span> <span class="s1">&#39;SR&#39;</span>
    <span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="n">FRESHMAN</span><span class="p">,</span> <span class="s1">&#39;Freshman&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">SOPHOMORE</span><span class="p">,</span> <span class="s1">&#39;Sophomore&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">JUNIOR</span><span class="p">,</span> <span class="s1">&#39;Junior&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="n">SENIOR</span><span class="p">,</span> <span class="s1">&#39;Senior&#39;</span><span class="p">),</span>
    <span class="p">)</span>
    <span class="n">year_in_school</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
                                      <span class="n">choices</span><span class="o">=</span><span class="n">YEAR_IN_SCHOOL_CHOICES</span><span class="p">,</span>
                                      <span class="n">default</span><span class="o">=</span><span class="n">FRESHMAN</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">is_upperclass</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_in_school</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">JUNIOR</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">SENIOR</span><span class="p">)</span>
</pre></div>
</div>
<p>Though you can define a choices list outside of a model class and then
refer to it, defining the choices and names for each choice inside the
model class keeps all of that information with the class that uses it,
and makes the choices easy to reference (e.g, <code class="docutils literal"><span class="pre">Student.SOPHOMORE</span></code>
will work anywhere that the <code class="docutils literal"><span class="pre">Student</span></code> model has been imported).</p>
<p>You can also collect your available choices into named groups that can
be used for organizational purposes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">MEDIA_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s1">&#39;Audio&#39;</span><span class="p">,</span> <span class="p">(</span>
            <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>
            <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>
        <span class="p">)</span>
    <span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;Video&#39;</span><span class="p">,</span> <span class="p">(</span>
            <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>
            <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>
        <span class="p">)</span>
    <span class="p">),</span>
    <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>
<span class="p">)</span>
</pre></div>
</div>
<p>The first element in each tuple is the name to apply to the group. The
second element is an iterable of 2-tuples, with each 2-tuple containing
a value and a human-readable name for an option. Grouped options may be
combined with ungrouped options within a single list (such as the
<cite>unknown</cite> option in this example).</p>
<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"><span class="pre">choices</span></code></a> set, Django will add a
method to retrieve the human-readable name for the field&#8217;s current value. See
<a class="reference internal" href="instances.html#django.db.models.Model.get_FOO_display" title="django.db.models.Model.get_FOO_display"><code class="xref py py-meth docutils literal"><span class="pre">get_FOO_display()</span></code></a> in the database API
documentation.</p>
<p>Note that choices can be any iterable object &#8211; not necessarily a list or tuple.
This lets you construct choices dynamically. But if you find yourself hacking
<a class="reference internal" href="#django.db.models.Field.choices" title="django.db.models.Field.choices"><code class="xref py py-attr docutils literal"><span class="pre">choices</span></code></a> to be dynamic, you&#8217;re probably better off using a 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"><span class="pre">ForeignKey</span></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"><span class="pre">choices</span></code></a> is meant for
static data that doesn&#8217;t change much, if ever.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<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"><span class="pre">blank=False</span></code></a> is set on the field along with a
<a class="reference internal" href="#django.db.models.Field.default" title="django.db.models.Field.default"><code class="xref py py-attr docutils literal"><span class="pre">default</span></code></a> then a label containing <code class="docutils literal"><span class="pre">&quot;---------&quot;</span></code> will be rendered
with the select box. To override this behavior, add a tuple to <code class="docutils literal"><span class="pre">choices</span></code>
containing <code class="docutils literal"><span class="pre">None</span></code>; e.g. <code class="docutils literal"><span class="pre">(None,</span> <span class="pre">'Your</span> <span class="pre">String</span> <span class="pre">For</span> <span class="pre">Display')</span></code>.
Alternatively, you can use an empty string instead of <code class="docutils literal"><span class="pre">None</span></code> where this makes
sense - 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"><span class="pre">CharField</span></code></a>.</p>
</div>
<div class="section" id="s-db-column">
<span id="db-column"></span><h3><code class="docutils literal"><span class="pre">db_column</span></code><a class="headerlink" href="#db-column" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_column">
<code class="descclassname">Field.</code><code class="descname">db_column</code><a class="headerlink" href="#django.db.models.Field.db_column" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the database column to use for this field. If this isn&#8217;t given,
Django will use the field&#8217;s name.</p>
<p>If your database column name is an SQL reserved word, or contains
characters that aren&#8217;t allowed in Python variable names &#8211; notably, the
hyphen &#8211; that&#8217;s OK. Django quotes column and table names behind the
scenes.</p>
</div>
<div class="section" id="s-db-index">
<span id="db-index"></span><h3><code class="docutils literal"><span class="pre">db_index</span></code><a class="headerlink" href="#db-index" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_index">
<code class="descclassname">Field.</code><code class="descname">db_index</code><a class="headerlink" href="#django.db.models.Field.db_index" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">True</span></code>, <a class="reference internal" href="../django-admin.html#django-admin-sqlindexes"><code class="xref std std-djadmin docutils literal"><span class="pre">django-admin</span> <span class="pre">sqlindexes</span></code></a> will output a
<code class="docutils literal"><span class="pre">CREATE</span> <span class="pre">INDEX</span></code> statement for this field.</p>
</div>
<div class="section" id="s-db-tablespace">
<span id="db-tablespace"></span><h3><code class="docutils literal"><span class="pre">db_tablespace</span></code><a class="headerlink" href="#db-tablespace" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_tablespace">
<code class="descclassname">Field.</code><code class="descname">db_tablespace</code><a class="headerlink" href="#django.db.models.Field.db_tablespace" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the <a class="reference internal" href="../../topics/db/tablespaces.html"><span class="doc">database tablespace</span></a> to use for
this field&#8217;s index, if this field is indexed. The default is the project&#8217;s
<a class="reference internal" href="../settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></code></a> setting, if set, or the
<a class="reference internal" href="options.html#django.db.models.Options.db_tablespace" title="django.db.models.Options.db_tablespace"><code class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></code></a> of the model, if any. If the backend doesn&#8217;t
support tablespaces for indexes, this option is ignored.</p>
</div>
<div class="section" id="s-default">
<span id="default"></span><h3><code class="docutils literal"><span class="pre">default</span></code><a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.default">
<code class="descclassname">Field.</code><code class="descname">default</code><a class="headerlink" href="#django.db.models.Field.default" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The default value for the field. This can be a value or a callable object. If
callable it will be called every time a new object is created.</p>
<p>The default cannot be a mutable object (model instance, list, set, etc.), as a
reference to the same instance of that object would be used as the default
value in all new model instances. Instead, wrap the desired default in a
callable.  For example, if you had a custom <code class="docutils literal"><span class="pre">JSONField</span></code> and wanted to specify
a dictionary as the default, use a function as follows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">contact_default</span><span class="p">():</span>
    <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>

<span class="n">contact_info</span> <span class="o">=</span> <span class="n">JSONField</span><span class="p">(</span><span class="s2">&quot;ContactInfo&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">contact_default</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal"><span class="pre">lambda</span></code>s cannot be used for field options like <code class="docutils literal"><span class="pre">default</span></code>
because they cannot be <a class="reference internal" href="../../topics/migrations.html#migration-serializing"><span class="std std-ref">serialized by migrations</span></a>.
See that documentation for other caveats.</p>
<p>The default value is used when new model instances are created and a value
isn&#8217;t provided for the field. When the field is a primary key, the default is
also used when the field is set to <code class="docutils literal"><span class="pre">None</span></code>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The default wasn&#8217;t used for <code class="docutils literal"><span class="pre">None</span></code> primary key values in previous
versions.</p>
</div>
</div>
<div class="section" id="s-editable">
<span id="editable"></span><h3><code class="docutils literal"><span class="pre">editable</span></code><a class="headerlink" href="#editable" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.editable">
<code class="descclassname">Field.</code><code class="descname">editable</code><a class="headerlink" href="#django.db.models.Field.editable" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">False</span></code>, the field will not be displayed in the admin or any other
<a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal"><span class="pre">ModelForm</span></code></a>. They are also skipped during <a class="reference internal" href="instances.html#validating-objects"><span class="std std-ref">model
validation</span></a>. Default is <code class="docutils literal"><span class="pre">True</span></code>.</p>
</div>
<div class="section" id="s-error-messages">
<span id="error-messages"></span><h3><code class="docutils literal"><span class="pre">error_messages</span></code><a class="headerlink" href="#error-messages" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.error_messages">
<code class="descclassname">Field.</code><code class="descname">error_messages</code><a class="headerlink" href="#django.db.models.Field.error_messages" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <code class="docutils literal"><span class="pre">error_messages</span></code> argument lets you override the default messages that the
field will raise. Pass in a dictionary with keys matching the error messages you
want to override.</p>
<p>Error message keys include <code class="docutils literal"><span class="pre">null</span></code>, <code class="docutils literal"><span class="pre">blank</span></code>, <code class="docutils literal"><span class="pre">invalid</span></code>, <code class="docutils literal"><span class="pre">invalid_choice</span></code>,
<code class="docutils literal"><span class="pre">unique</span></code>, and <code class="docutils literal"><span class="pre">unique_for_date</span></code>. Additional error message keys are
specified for each field in the <a class="reference internal" href="#field-types">Field types</a> section below.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>The <code class="docutils literal"><span class="pre">unique_for_date</span></code> error message key was added.</p>
</div>
<div class="section" id="s-help-text">
<span id="help-text"></span><h3><code class="docutils literal"><span class="pre">help_text</span></code><a class="headerlink" href="#help-text" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.help_text">
<code class="descclassname">Field.</code><code class="descname">help_text</code><a class="headerlink" href="#django.db.models.Field.help_text" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Extra &#8220;help&#8221; text to be displayed with the form widget. It&#8217;s useful for
documentation even if your field isn&#8217;t used on a form.</p>
<p>Note that this value is <em>not</em> HTML-escaped in automatically-generated
forms. 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"><span class="pre">help_text</span></code></a> if you so
desire. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">help_text</span><span class="o">=</span><span class="s2">&quot;Please use the following format: &lt;em&gt;YYYY-MM-DD&lt;/em&gt;.&quot;</span>
</pre></div>
</div>
<p>Alternatively you can use plain text and
<code class="docutils literal"><span class="pre">django.utils.html.escape()</span></code> to escape any HTML special characters. Ensure
that you escape any help text that may come from untrusted users to avoid a
cross-site scripting attack.</p>
</div>
<div class="section" id="s-primary-key">
<span id="primary-key"></span><h3><code class="docutils literal"><span class="pre">primary_key</span></code><a class="headerlink" href="#primary-key" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.primary_key">
<code class="descclassname">Field.</code><code class="descname">primary_key</code><a class="headerlink" href="#django.db.models.Field.primary_key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">True</span></code>, this field is the primary key for the model.</p>
<p>If you don&#8217;t specify <code class="docutils literal"><span class="pre">primary_key=True</span></code> for any field in your model, Django
will 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"><span class="pre">AutoField</span></code></a> to hold the primary key, so you
don&#8217;t need to set <code class="docutils literal"><span class="pre">primary_key=True</span></code> on any of your fields unless you want to
override the default primary-key behavior. For more, see
<a class="reference internal" href="../../topics/db/models.html#automatic-primary-key-fields"><span class="std std-ref">Automatic primary key fields</span></a>.</p>
<p><code class="docutils literal"><span class="pre">primary_key=True</span></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"><span class="pre">null=False</span></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"><span class="pre">unique=True</span></code></a>. Only one primary key is allowed on an
object.</p>
<p>The primary key field is read-only. If you change the value of the primary
key on an existing object and then save it, a new object will be created
alongside the old one.</p>
</div>
<div class="section" id="s-unique">
<span id="unique"></span><h3><code class="docutils literal"><span class="pre">unique</span></code><a class="headerlink" href="#unique" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique">
<code class="descclassname">Field.</code><code class="descname">unique</code><a class="headerlink" href="#django.db.models.Field.unique" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <code class="docutils literal"><span class="pre">True</span></code>, this field must be unique throughout the table.</p>
<p>This is enforced at the database level and by model validation. If
you 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"><span class="pre">unique</span></code></a>
field, a <a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><code class="xref py py-exc docutils literal"><span class="pre">django.db.IntegrityError</span></code></a> will be raised by the model&#8217;s
<a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal"><span class="pre">save()</span></code></a> method.</p>
<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"><span class="pre">ManyToManyField</span></code></a>,
<a class="reference internal" href="#django.db.models.OneToOneField" title="django.db.models.OneToOneField"><code class="xref py py-class docutils literal"><span class="pre">OneToOneField</span></code></a>, and <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal"><span class="pre">FileField</span></code></a>.</p>
<p>Note that when <code class="docutils literal"><span class="pre">unique</span></code> is <code class="docutils literal"><span class="pre">True</span></code>, you don&#8217;t need to specify
<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"><span class="pre">db_index</span></code></a>, because <code class="docutils literal"><span class="pre">unique</span></code> implies the creation of an index.</p>
</div>
<div class="section" id="s-unique-for-date">
<span id="unique-for-date"></span><h3><code class="docutils literal"><span class="pre">unique_for_date</span></code><a class="headerlink" href="#unique-for-date" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_date">
<code class="descclassname">Field.</code><code class="descname">unique_for_date</code><a class="headerlink" href="#django.db.models.Field.unique_for_date" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">DateField</span></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"><span class="pre">DateTimeField</span></code></a> to
require that this field be unique for the value of the date field.</p>
<p>For example, if you have a field <code class="docutils literal"><span class="pre">title</span></code> that has
<code class="docutils literal"><span class="pre">unique_for_date=&quot;pub_date&quot;</span></code>, then Django wouldn&#8217;t allow the entry of two
records with the same <code class="docutils literal"><span class="pre">title</span></code> and <code class="docutils literal"><span class="pre">pub_date</span></code>.</p>
<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"><span class="pre">DateTimeField</span></code></a>, only the date
portion of the field will be considered. Besides, when <a class="reference internal" href="../settings.html#std:setting-USE_TZ"><code class="xref std std-setting docutils literal"><span class="pre">USE_TZ</span></code></a> is
<code class="docutils literal"><span class="pre">True</span></code>, the check will be performed in the <a class="reference internal" href="../../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">current time zone</span></a> at the time the object gets saved.</p>
<p>This is enforced by <a class="reference internal" href="instances.html#django.db.models.Model.validate_unique" title="django.db.models.Model.validate_unique"><code class="xref py py-meth docutils literal"><span class="pre">Model.validate_unique()</span></code></a> during model validation
but 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"><span class="pre">unique_for_date</span></code></a> constraint
involves fields that are not part of a <a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal"><span class="pre">ModelForm</span></code></a> (for
example, if one of the fields is listed in <code class="docutils literal"><span class="pre">exclude</span></code> or has
<a class="reference internal" href="#django.db.models.Field.editable" title="django.db.models.Field.editable"><code class="xref py py-attr docutils literal"><span class="pre">editable=False</span></code></a>), <a class="reference internal" href="instances.html#django.db.models.Model.validate_unique" title="django.db.models.Model.validate_unique"><code class="xref py py-meth docutils literal"><span class="pre">Model.validate_unique()</span></code></a> will
skip validation for that particular constraint.</p>
</div>
<div class="section" id="s-unique-for-month">
<span id="unique-for-month"></span><h3><code class="docutils literal"><span class="pre">unique_for_month</span></code><a class="headerlink" href="#unique-for-month" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_month">
<code class="descclassname">Field.</code><code class="descname">unique_for_month</code><a class="headerlink" href="#django.db.models.Field.unique_for_month" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">unique_for_date</span></code></a>, but requires the field to be unique with
respect to the month.</p>
</div>
<div class="section" id="s-unique-for-year">
<span id="unique-for-year"></span><h3><code class="docutils literal"><span class="pre">unique_for_year</span></code><a class="headerlink" href="#unique-for-year" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique_for_year">
<code class="descclassname">Field.</code><code class="descname">unique_for_year</code><a class="headerlink" href="#django.db.models.Field.unique_for_year" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">unique_for_date</span></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"><span class="pre">unique_for_month</span></code></a>.</p>
</div>
<div class="section" id="s-verbose-name">
<span id="verbose-name"></span><h3><code class="docutils literal"><span class="pre">verbose_name</span></code><a class="headerlink" href="#verbose-name" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.verbose_name">
<code class="descclassname">Field.</code><code class="descname">verbose_name</code><a class="headerlink" href="#django.db.models.Field.verbose_name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A human-readable name for the field. If the verbose name isn&#8217;t given, Django
will automatically create it using the field&#8217;s attribute name, converting
underscores to spaces. See <a class="reference internal" href="../../topics/db/models.html#verbose-field-names"><span class="std std-ref">Verbose field names</span></a>.</p>
</div>
<div class="section" id="s-validators">
<span id="validators"></span><h3><code class="docutils literal"><span class="pre">validators</span></code><a class="headerlink" href="#validators" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.validators">
<code class="descclassname">Field.</code><code class="descname">validators</code><a class="headerlink" href="#django.db.models.Field.validators" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A list of validators to run for this field. See the <a class="reference internal" href="../validators.html"><span class="doc">validators
documentation</span></a> for more information.</p>
<div class="section" id="s-registering-and-fetching-lookups">
<span id="registering-and-fetching-lookups"></span><h4>Registering and fetching lookups<a class="headerlink" href="#registering-and-fetching-lookups" title="Permalink to this headline">¶</a></h4>
<p><code class="docutils literal"><span class="pre">Field</span></code> implements the <a class="reference internal" href="lookups.html#lookup-registration-api"><span class="std std-ref">lookup registration API</span></a>.
The API can be used to customize which lookups are available for a field class, and
how lookups are fetched from a field.</p>
</div>
</div>
</div>
<div class="section" id="s-field-types">
<span id="s-model-field-types"></span><span id="field-types"></span><span id="model-field-types"></span><h2>Field types<a class="headerlink" href="#field-types" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-autofield">
<span id="autofield"></span><h3><code class="docutils literal"><span class="pre">AutoField</span></code><a class="headerlink" href="#autofield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.AutoField">
<em class="property">class </em><code class="descname">AutoField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#AutoField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.AutoField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><code class="xref py py-class docutils literal"><span class="pre">IntegerField</span></code></a> that automatically increments
according to available IDs. You usually won&#8217;t need to use this directly; a
primary key field will automatically be added to your model if you don&#8217;t specify
otherwise. See <a class="reference internal" href="../../topics/db/models.html#automatic-primary-key-fields"><span class="std std-ref">Automatic primary key fields</span></a>.</p>
</div>
<div class="section" id="s-bigintegerfield">
<span id="bigintegerfield"></span><h3><code class="docutils literal"><span class="pre">BigIntegerField</span></code><a class="headerlink" href="#bigintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BigIntegerField">
<em class="property">class </em><code class="descname">BigIntegerField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#BigIntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.BigIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">IntegerField</span></code></a> except that it is
guaranteed to fit numbers from <code class="docutils literal"><span class="pre">-9223372036854775808</span></code> to
<code class="docutils literal"><span class="pre">9223372036854775807</span></code>. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.</p>
</div>
<div class="section" id="s-binaryfield">
<span id="binaryfield"></span><h3><code class="docutils literal"><span class="pre">BinaryField</span></code><a class="headerlink" href="#binaryfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BinaryField">
<em class="property">class </em><code class="descname">BinaryField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#BinaryField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.BinaryField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A field to store raw binary data. It only supports <code class="docutils literal"><span class="pre">bytes</span></code> assignment. Be
aware that this field has limited functionality. For example, it is not possible
to filter a queryset on a <code class="docutils literal"><span class="pre">BinaryField</span></code> value.</p>
<div class="admonition-abusing-binaryfield admonition">
<p class="first admonition-title">Abusing <code class="docutils literal"><span class="pre">BinaryField</span></code></p>
<p class="last">Although you might think about storing files in the database, consider that
it is bad design in 99% of the cases. This field is <em>not</em> a replacement for
proper <a class="reference internal" href="../../howto/static-files/index.html"><span class="doc">static files</span></a> handling.</p>
</div>
</div>
<div class="section" id="s-booleanfield">
<span id="booleanfield"></span><h3><code class="docutils literal"><span class="pre">BooleanField</span></code><a class="headerlink" href="#booleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.BooleanField">
<em class="property">class </em><code class="descname">BooleanField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#BooleanField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.BooleanField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A true/false field.</p>
<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.CheckboxInput" title="django.forms.CheckboxInput"><code class="xref py py-class docutils literal"><span class="pre">CheckboxInput</span></code></a>.</p>
<p>If you need to accept <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><code class="xref py py-attr docutils literal"><span class="pre">null</span></code></a> values then use
<a class="reference internal" href="#django.db.models.NullBooleanField" title="django.db.models.NullBooleanField"><code class="xref py py-class docutils literal"><span class="pre">NullBooleanField</span></code></a> instead.</p>
<p>The default value of <code class="docutils literal"><span class="pre">BooleanField</span></code> is <code class="docutils literal"><span class="pre">None</span></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"><span class="pre">Field.default</span></code></a>
isn&#8217;t defined.</p>
</div>
<div class="section" id="s-charfield">
<span id="charfield"></span><h3><code class="docutils literal"><span class="pre">CharField</span></code><a class="headerlink" href="#charfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.CharField">
<em class="property">class </em><code class="descname">CharField</code>(<em>max_length=None</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#CharField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.CharField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A string field, for small- to large-sized strings.</p>
<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"><span class="pre">TextField</span></code></a>.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.</p>
<p><a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> has one extra required argument:</p>
<dl class="attribute">
<dt id="django.db.models.CharField.max_length">
<code class="descclassname">CharField.</code><code class="descname">max_length</code><a class="headerlink" href="#django.db.models.CharField.max_length" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum length (in characters) of the field. The max_length is enforced
at the database level and in Django&#8217;s validation.</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are writing an application that must be portable to multiple
database backends, you should be aware that there are restrictions on
<code class="docutils literal"><span class="pre">max_length</span></code> for some backends. Refer to the <a class="reference internal" href="../databases.html"><span class="doc">database backend
notes</span></a> for details.</p>
</div>
<div class="admonition-mysql-users admonition">
<p class="first admonition-title">MySQL users</p>
<p class="last">If you are using this field with MySQLdb 1.2.2 and the <code class="docutils literal"><span class="pre">utf8_bin</span></code>
collation (which is <em>not</em> the default), there are some issues to be aware
of. Refer to the <a class="reference internal" href="../databases.html#mysql-collation"><span class="std std-ref">MySQL database notes</span></a> for
details.</p>
</div>
</div>
<div class="section" id="s-commaseparatedintegerfield">
<span id="commaseparatedintegerfield"></span><h3><code class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></code><a class="headerlink" href="#commaseparatedintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.CommaSeparatedIntegerField">
<em class="property">class </em><code class="descname">CommaSeparatedIntegerField</code>(<em>max_length=None</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#CommaSeparatedIntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.CommaSeparatedIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A field of integers separated by commas. As in <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a>, 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"><span class="pre">max_length</span></code></a> argument is required and the note about database
portability mentioned there should be heeded.</p>
</div>
<div class="section" id="s-datefield">
<span id="datefield"></span><h3><code class="docutils literal"><span class="pre">DateField</span></code><a class="headerlink" href="#datefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DateField">
<em class="property">class </em><code class="descname">DateField</code>(<em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#DateField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.DateField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A date, represented in Python by a <code class="docutils literal"><span class="pre">datetime.date</span></code> instance. Has a few extra,
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DateField.auto_now">
<code class="descclassname">DateField.</code><code class="descname">auto_now</code><a class="headerlink" href="#django.db.models.DateField.auto_now" title="Permalink to this definition">¶</a></dt>
<dd><p>Automatically set the field to now every time the object is saved. Useful
for &#8220;last-modified&#8221; timestamps. Note that the current date is <em>always</em>
used; it&#8217;s not just a default value that you can override.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.DateField.auto_now_add">
<code class="descclassname">DateField.</code><code class="descname">auto_now_add</code><a class="headerlink" href="#django.db.models.DateField.auto_now_add" title="Permalink to this definition">¶</a></dt>
<dd><p>Automatically set the field to now when the object is first created. Useful
for creation of timestamps. Note that the current date is <em>always</em> used;
it&#8217;s not just a default value that you can override. So even if you
set a value for this field when creating the object, it will be ignored.
If you want to be able to modify this field, set the following instead of
<code class="docutils literal"><span class="pre">auto_now_add=True</span></code>:</p>
<ul class="simple">
<li>For <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><code class="xref py py-class docutils literal"><span class="pre">DateField</span></code></a>: <code class="docutils literal"><span class="pre">default=date.today</span></code> - from
<code class="xref py py-meth docutils literal"><span class="pre">datetime.date.today()</span></code></li>
<li>For <a class="reference internal" href="#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><code class="xref py py-class docutils literal"><span class="pre">DateTimeField</span></code></a>: <code class="docutils literal"><span class="pre">default=timezone.now</span></code> - from
<a class="reference internal" href="../utils.html#django.utils.timezone.now" title="django.utils.timezone.now"><code class="xref py py-func docutils literal"><span class="pre">django.utils.timezone.now()</span></code></a></li>
</ul>
</dd></dl>

<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>. The admin adds a JavaScript calendar,
and a shortcut for &#8220;Today&#8221;. Includes an additional <code class="docutils literal"><span class="pre">invalid_date</span></code> error
message key.</p>
<p>The options <code class="docutils literal"><span class="pre">auto_now_add</span></code>, <code class="docutils literal"><span class="pre">auto_now</span></code>, and <code class="docutils literal"><span class="pre">default</span></code> are mutually exclusive.
Any combination of these options will result in an error.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">As currently implemented, setting <code class="docutils literal"><span class="pre">auto_now</span></code> or <code class="docutils literal"><span class="pre">auto_now_add</span></code> to
<code class="docutils literal"><span class="pre">True</span></code> will cause the field to have <code class="docutils literal"><span class="pre">editable=False</span></code> and <code class="docutils literal"><span class="pre">blank=True</span></code>
set.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">auto_now</span></code> and <code class="docutils literal"><span class="pre">auto_now_add</span></code> options will always use the date in
the <a class="reference internal" href="../../topics/i18n/timezones.html#default-current-time-zone"><span class="std std-ref">default timezone</span></a> at the moment of
creation or update. If you need something different, you may want to
consider simply using your own callable default or overriding <code class="docutils literal"><span class="pre">save()</span></code>
instead of using <code class="docutils literal"><span class="pre">auto_now</span></code> or <code class="docutils literal"><span class="pre">auto_now_add</span></code>; or using a
<code class="docutils literal"><span class="pre">DateTimeField</span></code> instead of a <code class="docutils literal"><span class="pre">DateField</span></code> and deciding how to handle the
conversion from datetime to date at display time.</p>
</div>
</div>
<div class="section" id="s-datetimefield">
<span id="datetimefield"></span><h3><code class="docutils literal"><span class="pre">DateTimeField</span></code><a class="headerlink" href="#datetimefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DateTimeField">
<em class="property">class </em><code class="descname">DateTimeField</code>(<em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#DateTimeField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.DateTimeField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A date and time, represented in Python by a <code class="docutils literal"><span class="pre">datetime.datetime</span></code> instance.
Takes 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"><span class="pre">DateField</span></code></a>.</p>
<p>The default form widget for this field is a single
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>. The admin uses two separate
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a> widgets with JavaScript shortcuts.</p>
</div>
<div class="section" id="s-decimalfield">
<span id="decimalfield"></span><h3><code class="docutils literal"><span class="pre">DecimalField</span></code><a class="headerlink" href="#decimalfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.DecimalField">
<em class="property">class </em><code class="descname">DecimalField</code>(<em>max_digits=None</em>, <em>decimal_places=None</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#DecimalField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.DecimalField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A fixed-precision decimal number, represented in Python by a
<code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code> instance. Has two <strong>required</strong> arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DecimalField.max_digits">
<code class="descclassname">DecimalField.</code><code class="descname">max_digits</code><a class="headerlink" href="#django.db.models.DecimalField.max_digits" title="Permalink to this definition">¶</a></dt>
<dd><p>The maximum number of digits allowed in the number. Note that this number
must be greater than or equal to <code class="docutils literal"><span class="pre">decimal_places</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.DecimalField.decimal_places">
<code class="descclassname">DecimalField.</code><code class="descname">decimal_places</code><a class="headerlink" href="#django.db.models.DecimalField.decimal_places" title="Permalink to this definition">¶</a></dt>
<dd><p>The number of decimal places to store with the number.</p>
</dd></dl>

<p>For example, to store numbers up to <code class="docutils literal"><span class="pre">999</span></code> with a resolution of 2 decimal
places, you&#8217;d use:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">max_digits</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
<p>And to store numbers up to approximately one billion with a resolution of 10
decimal places:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span><span class="p">(</span><span class="o">...</span><span class="p">,</span> <span class="n">max_digits</span><span class="o">=</span><span class="mi">19</span><span class="p">,</span> <span class="n">decimal_places</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><code class="xref py py-class docutils literal"><span class="pre">NumberInput</span></code></a>
when <a class="reference internal" href="../forms/fields.html#django.forms.Field.localize" title="django.forms.Field.localize"><code class="xref py py-attr docutils literal"><span class="pre">localize</span></code></a> is <code class="docutils literal"><span class="pre">False</span></code> or
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a> otherwise.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For more information about the differences between the
<a class="reference internal" href="#django.db.models.FloatField" title="django.db.models.FloatField"><code class="xref py py-class docutils literal"><span class="pre">FloatField</span></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"><span class="pre">DecimalField</span></code></a> classes, please
see <a class="reference internal" href="#floatfield-vs-decimalfield"><span class="std std-ref">FloatField vs. DecimalField</span></a>.</p>
</div>
</div>
<div class="section" id="s-durationfield">
<span id="durationfield"></span><h3><code class="docutils literal"><span class="pre">DurationField</span></code><a class="headerlink" href="#durationfield" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<dl class="class">
<dt id="django.db.models.DurationField">
<em class="property">class </em><code class="descname">DurationField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#DurationField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.DurationField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A field for storing periods of time - modeled in Python by
<code class="xref py py-class docutils literal"><span class="pre">timedelta</span></code>. When used on PostgreSQL, the data type
used is an <code class="docutils literal"><span class="pre">interval</span></code> and on Oracle the data type is <code class="docutils literal"><span class="pre">INTERVAL</span> <span class="pre">DAY(9)</span> <span class="pre">TO</span>
<span class="pre">SECOND(6)</span></code>. Otherwise a <code class="docutils literal"><span class="pre">bigint</span></code> of microseconds is used.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Arithmetic with <code class="docutils literal"><span class="pre">DurationField</span></code> works in most cases. However on all
databases other than PostgreSQL, comparing the value of a <code class="docutils literal"><span class="pre">DurationField</span></code>
to arithmetic on <code class="docutils literal"><span class="pre">DateTimeField</span></code> instances will not work as expected.</p>
</div>
</div>
<div class="section" id="s-emailfield">
<span id="emailfield"></span><h3><code class="docutils literal"><span class="pre">EmailField</span></code><a class="headerlink" href="#emailfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.EmailField">
<em class="property">class </em><code class="descname">EmailField</code>(<em>max_length=254</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#EmailField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.EmailField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> that checks that the value is a valid email address. It
uses <a class="reference internal" href="../validators.html#django.core.validators.EmailValidator" title="django.core.validators.EmailValidator"><code class="xref py py-class docutils literal"><span class="pre">EmailValidator</span></code></a> to validate the input.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The default <code class="docutils literal"><span class="pre">max_length</span></code> was increased from 75 to 254 in order to be
compliant with RFC3696/5321.</p>
</div>
</div>
<div class="section" id="s-filefield">
<span id="filefield"></span><h3><code class="docutils literal"><span class="pre">FileField</span></code><a class="headerlink" href="#filefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FileField">
<em class="property">class </em><code class="descname">FileField</code>(<em>upload_to=None</em>, <em>max_length=100</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FileField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.FileField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A file-upload field.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal"><span class="pre">primary_key</span></code> and <code class="docutils literal"><span class="pre">unique</span></code> arguments are not supported, and will
raise a <code class="docutils literal"><span class="pre">TypeError</span></code> if used.</p>
</div>
<p>Has two optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.FileField.upload_to">
<code class="descclassname">FileField.</code><code class="descname">upload_to</code><a class="headerlink" href="#django.db.models.FileField.upload_to" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p><code class="docutils literal"><span class="pre">upload_to</span></code> was required in older versions of Django.</p>
</div>
<p>A local filesystem path that will be appended to your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></code></a>
setting to determine the value of the
<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"><span class="pre">url</span></code></a> attribute.</p>
<p>This path may contain <code class="xref py py-func docutils literal"><span class="pre">strftime()</span></code> formatting, which will be
replaced by the date/time of the file upload (so that uploaded files don&#8217;t
fill up the given directory).</p>
<p>This may also be a callable, such as a function, which will be called to
obtain the upload path, including the filename. This callable must be able
to accept two arguments, and return a Unix-style path (with forward slashes)
to be passed along to the storage system. The two arguments that will be
passed are:</p>
<table class="docutils">
<colgroup>
<col width="32%" />
<col width="68%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Argument</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">instance</span></code></td>
<td><p class="first">An instance of the model where the
<code class="docutils literal"><span class="pre">FileField</span></code> is defined. More specifically,
this is the particular instance where the
current file is being attached.</p>
<p class="last">In most cases, this object will not have been
saved to the database yet, so if it uses the
default <code class="docutils literal"><span class="pre">AutoField</span></code>, <em>it might not yet have a
value for its primary key field</em>.</p>
</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">filename</span></code></td>
<td>The filename that was originally given to the
file. This may or may not be taken into account
when determining the final destination path.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FileField.storage">
<code class="descclassname">FileField.</code><code class="descname">storage</code><a class="headerlink" href="#django.db.models.FileField.storage" title="Permalink to this definition">¶</a></dt>
<dd><p>A storage object, which handles the storage and retrieval of your
files. See <a class="reference internal" href="../../topics/files.html"><span class="doc">Managing files</span></a> for details on how to provide this object.</p>
</dd></dl>

<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><code class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></code></a>.</p>
<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"><span class="pre">FileField</span></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"><span class="pre">ImageField</span></code></a> (see below) in a model
takes a few steps:</p>
<ol class="arabic simple">
<li>In your settings file, you&#8217;ll need to define <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></code></a> as the
full path to a directory where you&#8217;d like Django to store uploaded files.
(For performance, these files are not stored in the database.) Define
<a class="reference internal" href="../settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></code></a> as the base public URL of that directory. Make sure
that this directory is writable by the Web server&#8217;s user account.</li>
<li>Add the <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal"><span class="pre">FileField</span></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"><span class="pre">ImageField</span></code></a> to your model, defining
the <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"><span class="pre">upload_to</span></code></a> option to specify a subdirectory of
<a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></code></a> to use for uploaded files.</li>
<li>All that will be stored in your database is a path to the file
(relative to <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></code></a>). You&#8217;ll most likely want to use the
convenience <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"><span class="pre">url</span></code></a> attribute
provided 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"><span class="pre">ImageField</span></code></a> is called
<code class="docutils literal"><span class="pre">mug_shot</span></code>, you can get the absolute path to your image in a template with
<code class="docutils literal"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></code>.</li>
</ol>
<p>For example, say your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></code></a> is set to <code class="docutils literal"><span class="pre">'/home/media'</span></code>, and
<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"><span class="pre">upload_to</span></code></a> is set to <code class="docutils literal"><span class="pre">'photos/%Y/%m/%d'</span></code>. The <code class="docutils literal"><span class="pre">'%Y/%m/%d'</span></code>
part 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"><span class="pre">upload_to</span></code></a> is <code class="xref py py-func docutils literal"><span class="pre">strftime()</span></code> formatting;
<code class="docutils literal"><span class="pre">'%Y'</span></code> is the four-digit year, <code class="docutils literal"><span class="pre">'%m'</span></code> is the two-digit month and <code class="docutils literal"><span class="pre">'%d'</span></code> is
the two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in
the directory <code class="docutils literal"><span class="pre">/home/media/photos/2007/01/15</span></code>.</p>
<p>If you wanted to retrieve the uploaded file&#8217;s on-disk filename, or the file&#8217;s
size, you could use the <a class="reference internal" href="../files/file.html#django.core.files.File.name" title="django.core.files.File.name"><code class="xref py py-attr docutils literal"><span class="pre">name</span></code></a> and
<a class="reference internal" href="../files/file.html#django.core.files.File.size" title="django.core.files.File.size"><code class="xref py py-attr docutils literal"><span class="pre">size</span></code></a> attributes respectively; for more
information on the available attributes and methods, see the
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal"><span class="pre">File</span></code></a> class reference and the <a class="reference internal" href="../../topics/files.html"><span class="doc">Managing files</span></a>
topic guide.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The file is saved as part of saving the model in the database, so the actual
file name used on disk cannot be relied on until after the model has been
saved.</p>
</div>
<p>The uploaded file&#8217;s relative URL can be obtained using the
<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"><span class="pre">url</span></code></a> attribute. Internally,
this calls the <a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><code class="xref py py-meth docutils literal"><span class="pre">url()</span></code></a> method of the
underlying <a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><code class="xref py py-class docutils literal"><span class="pre">Storage</span></code></a> class.</p>
<p id="file-upload-security">Note that whenever you deal with uploaded files, you should pay close attention
to where you&#8217;re uploading them and what type of files they are, to avoid
security holes. <em>Validate all uploaded files</em> so that you&#8217;re sure the files are
what you think they are. For example, if you blindly let somebody upload files,
without validation, to a directory that&#8217;s within your Web server&#8217;s document
root, then somebody could upload a CGI or PHP script and execute that script by
visiting its URL on your site. Don&#8217;t allow that.</p>
<p>Also note that even an uploaded HTML file, since it can be executed by the
browser (though not by the server), can pose security threats that are
equivalent to XSS or CSRF attacks.</p>
<p><a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal"><span class="pre">FileField</span></code></a> instances are created in your database as <code class="docutils literal"><span class="pre">varchar</span></code>
columns with a default max length of 100 characters. As with other fields, you
can 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"><span class="pre">max_length</span></code></a> argument.</p>
<div class="section" id="s-filefield-and-fieldfile">
<span id="filefield-and-fieldfile"></span><h4>FileField and FieldFile<a class="headerlink" href="#filefield-and-fieldfile" title="Permalink to this headline">¶</a></h4>
<dl class="class">
<dt id="django.db.models.fields.files.FieldFile">
<em class="property">class </em><code class="descname">FieldFile</code><a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">FileField</span></code></a> on a model, you are
given 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"><span class="pre">FieldFile</span></code></a> as a proxy for accessing the underlying
file. In addition to the functionality inherited from
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal"><span class="pre">django.core.files.File</span></code></a>, this class has several attributes and methods
that can be used to interact with file data:</p>
<dl class="attribute">
<dt id="django.db.models.fields.files.FieldFile.url">
<code class="descclassname">FieldFile.</code><code class="descname">url</code><a class="headerlink" href="#django.db.models.fields.files.FieldFile.url" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A read-only property to access the file&#8217;s relative URL by calling the
<a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><code class="xref py py-meth docutils literal"><span class="pre">url()</span></code></a> method of the underlying
<a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><code class="xref py py-class docutils literal"><span class="pre">Storage</span></code></a> class.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.open">
<code class="descclassname">FieldFile.</code><code class="descname">open</code>(<em>mode='rb'</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.open"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.open" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <code class="docutils literal"><span class="pre">open()</span></code> method and opens the file
associated with this instance in the mode specified by <code class="docutils literal"><span class="pre">mode</span></code>.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.close">
<code class="descclassname">FieldFile.</code><code class="descname">close</code>()<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.close"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.close" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <code class="docutils literal"><span class="pre">file.close()</span></code> method and closes the file
associated with this instance.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.save">
<code class="descclassname">FieldFile.</code><code class="descname">save</code>(<em>name</em>, <em>content</em>, <em>save=True</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.save"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.save" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This method takes a filename and file contents and passes them to the storage
class for the field, then associates the stored file with the model field.
If you want to manually associate file data with
<a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal"><span class="pre">FileField</span></code></a> instances on your model, the <code class="docutils literal"><span class="pre">save()</span></code>
method is used to persist that file data.</p>
<p>Takes two required arguments: <code class="docutils literal"><span class="pre">name</span></code> which is the name of the file, and
<code class="docutils literal"><span class="pre">content</span></code> which is an object containing the file&#8217;s contents.  The
optional <code class="docutils literal"><span class="pre">save</span></code> argument controls whether or not the model instance is
saved after the file associated with this field has been altered. Defaults to
<code class="docutils literal"><span class="pre">True</span></code>.</p>
<p>Note that the <code class="docutils literal"><span class="pre">content</span></code> argument should be an instance of
<a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal"><span class="pre">django.core.files.File</span></code></a>, not Python&#8217;s built-in file object.
You can construct a <a class="reference internal" href="../files/file.html#django.core.files.File" title="django.core.files.File"><code class="xref py py-class docutils literal"><span class="pre">File</span></code></a> from an existing
Python file object like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.files</span> <span class="k">import</span> <span class="n">File</span>
<span class="c1"># Open an existing file using Python&#39;s built-in open()</span>
<span class="n">f</span> <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>
<span class="n">myfile</span> <span class="o">=</span> <span class="n">File</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
</pre></div>
</div>
<p>Or you can construct one from a Python string like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.files.base</span> <span class="k">import</span> <span class="n">ContentFile</span>
<span class="n">myfile</span> <span class="o">=</span> <span class="n">ContentFile</span><span class="p">(</span><span class="s2">&quot;hello world&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>For more information, see <a class="reference internal" href="../../topics/files.html"><span class="doc">Managing files</span></a>.</p>
<dl class="method">
<dt id="django.db.models.fields.files.FieldFile.delete">
<code class="descclassname">FieldFile.</code><code class="descname">delete</code>(<em>save=True</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#FieldFile.delete"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.fields.files.FieldFile.delete" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Deletes the file associated with this instance and clears all attributes on
the field. Note: This method will close the file if it happens to be open when
<code class="docutils literal"><span class="pre">delete()</span></code> is called.</p>
<p>The optional <code class="docutils literal"><span class="pre">save</span></code> argument controls whether or not the model instance is
saved after the file associated with this field has been deleted. Defaults to
<code class="docutils literal"><span class="pre">True</span></code>.</p>
<p>Note that when a model is deleted, related files are not deleted. If you need
to cleanup orphaned files, you&#8217;ll need to handle it yourself (for instance,
with a custom management command that can be run manually or scheduled to run
periodically via e.g. cron).</p>
</div>
</div>
<div class="section" id="s-filepathfield">
<span id="filepathfield"></span><h3><code class="docutils literal"><span class="pre">FilePathField</span></code><a class="headerlink" href="#filepathfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FilePathField">
<em class="property">class </em><code class="descname">FilePathField</code>(<em>path=None</em>, <em>match=None</em>, <em>recursive=False</em>, <em>max_length=100</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#FilePathField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.FilePathField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> whose choices are limited to the filenames in a certain
directory on the filesystem. Has three special arguments, of which the first is
<strong>required</strong>:</p>
<dl class="attribute">
<dt id="django.db.models.FilePathField.path">
<code class="descclassname">FilePathField.</code><code class="descname">path</code><a class="headerlink" href="#django.db.models.FilePathField.path" title="Permalink to this definition">¶</a></dt>
<dd><p>Required. The absolute filesystem path to a directory from which this
<a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><code class="xref py py-class docutils literal"><span class="pre">FilePathField</span></code></a> should get its choices. Example: <code class="docutils literal"><span class="pre">&quot;/home/images&quot;</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.match">
<code class="descclassname">FilePathField.</code><code class="descname">match</code><a class="headerlink" href="#django.db.models.FilePathField.match" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">FilePathField</span></code></a>
will use to filter filenames. Note that the regex will be applied to the
base filename, not the full path. Example: <code class="docutils literal"><span class="pre">&quot;foo.*\.txt$&quot;</span></code>, which will
match a file called <code class="docutils literal"><span class="pre">foo23.txt</span></code> but not <code class="docutils literal"><span class="pre">bar.txt</span></code> or <code class="docutils literal"><span class="pre">foo23.png</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.recursive">
<code class="descclassname">FilePathField.</code><code class="descname">recursive</code><a class="headerlink" href="#django.db.models.FilePathField.recursive" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional. Either <code class="docutils literal"><span class="pre">True</span></code> or <code class="docutils literal"><span class="pre">False</span></code>. Default is <code class="docutils literal"><span class="pre">False</span></code>. Specifies
whether 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"><span class="pre">path</span></code></a> should be included</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.allow_files">
<code class="descclassname">FilePathField.</code><code class="descname">allow_files</code><a class="headerlink" href="#django.db.models.FilePathField.allow_files" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <code class="docutils literal"><span class="pre">True</span></code> or <code class="docutils literal"><span class="pre">False</span></code>.  Default is <code class="docutils literal"><span class="pre">True</span></code>.  Specifies
whether files in the specified location should be included.  Either this or
<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"><span class="pre">allow_folders</span></code></a> must be <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.FilePathField.allow_folders">
<code class="descclassname">FilePathField.</code><code class="descname">allow_folders</code><a class="headerlink" href="#django.db.models.FilePathField.allow_folders" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional.  Either <code class="docutils literal"><span class="pre">True</span></code> or <code class="docutils literal"><span class="pre">False</span></code>.  Default is <code class="docutils literal"><span class="pre">False</span></code>.  Specifies
whether folders in the specified location should be included.  Either this
or <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"><span class="pre">allow_files</span></code></a> must be <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>

<p>Of course, these arguments can be used together.</p>
<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"><span class="pre">match</span></code></a> applies to the
base filename, not the full path. So, this example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">FilePathField</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="s2">&quot;/home/images&quot;</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s2">&quot;foo.*&quot;</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
</div>
<p>...will match <code class="docutils literal"><span class="pre">/home/images/foo.png</span></code> but not <code class="docutils literal"><span class="pre">/home/images/foo/bar.png</span></code>
because 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"><span class="pre">match</span></code></a> applies to the base filename
(<code class="docutils literal"><span class="pre">foo.png</span></code> and <code class="docutils literal"><span class="pre">bar.png</span></code>).</p>
<p><a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><code class="xref py py-class docutils literal"><span class="pre">FilePathField</span></code></a> instances are created in your database as <code class="docutils literal"><span class="pre">varchar</span></code>
columns with a default max length of 100 characters. As with other fields, you
can 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"><span class="pre">max_length</span></code></a> argument.</p>
</div>
<div class="section" id="s-floatfield">
<span id="floatfield"></span><h3><code class="docutils literal"><span class="pre">FloatField</span></code><a class="headerlink" href="#floatfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.FloatField">
<em class="property">class </em><code class="descname">FloatField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#FloatField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.FloatField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A floating-point number represented in Python by a <code class="docutils literal"><span class="pre">float</span></code> instance.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><code class="xref py py-class docutils literal"><span class="pre">NumberInput</span></code></a>
when <a class="reference internal" href="../forms/fields.html#django.forms.Field.localize" title="django.forms.Field.localize"><code class="xref py py-attr docutils literal"><span class="pre">localize</span></code></a> is <code class="docutils literal"><span class="pre">False</span></code> or
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a> otherwise.</p>
<div class="admonition-floatfield-vs-decimalfield admonition" id="floatfield-vs-decimalfield">
<p class="first admonition-title"><code class="docutils literal"><span class="pre">FloatField</span></code> vs. <code class="docutils literal"><span class="pre">DecimalField</span></code></p>
<p class="last">The <a class="reference internal" href="#django.db.models.FloatField" title="django.db.models.FloatField"><code class="xref py py-class docutils literal"><span class="pre">FloatField</span></code></a> class is sometimes mixed up with the
<a class="reference internal" href="#django.db.models.DecimalField" title="django.db.models.DecimalField"><code class="xref py py-class docutils literal"><span class="pre">DecimalField</span></code></a> class. Although they both represent real numbers, they
represent those numbers differently. <code class="docutils literal"><span class="pre">FloatField</span></code> uses Python&#8217;s <code class="docutils literal"><span class="pre">float</span></code>
type internally, while <code class="docutils literal"><span class="pre">DecimalField</span></code> uses Python&#8217;s <code class="docutils literal"><span class="pre">Decimal</span></code> type. For
information on the difference between the two, see Python&#8217;s documentation
for the <code class="xref py py-mod docutils literal"><span class="pre">decimal</span></code> module.</p>
</div>
</div>
<div class="section" id="s-imagefield">
<span id="imagefield"></span><h3><code class="docutils literal"><span class="pre">ImageField</span></code><a class="headerlink" href="#imagefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ImageField">
<em class="property">class </em><code class="descname">ImageField</code>(<em>upload_to=None</em>, <em>height_field=None</em>, <em>width_field=None</em>, <em>max_length=100</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/files.html#ImageField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.ImageField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">FileField</span></code></a>, but also
validates that the uploaded object is a valid image.</p>
<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"><span class="pre">FileField</span></code></a>,
an <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal"><span class="pre">ImageField</span></code></a> also has <code class="docutils literal"><span class="pre">height</span></code> and <code class="docutils literal"><span class="pre">width</span></code> attributes.</p>
<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"><span class="pre">ImageField</span></code></a> has two extra
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.ImageField.height_field">
<code class="descclassname">ImageField.</code><code class="descname">height_field</code><a class="headerlink" href="#django.db.models.ImageField.height_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Name of a model field which will be auto-populated with the height of the
image each time the model instance is saved.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ImageField.width_field">
<code class="descclassname">ImageField.</code><code class="descname">width_field</code><a class="headerlink" href="#django.db.models.ImageField.width_field" title="Permalink to this definition">¶</a></dt>
<dd><p>Name of a model field which will be auto-populated with the width of the
image each time the model instance is saved.</p>
</dd></dl>

<p>Requires the <a class="reference external" href="http://pillow.readthedocs.org/en/latest/">Pillow</a> library.</p>
<p><a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal"><span class="pre">ImageField</span></code></a> instances are created in your database as <code class="docutils literal"><span class="pre">varchar</span></code>
columns with a default max length of 100 characters. As with other fields, you
can 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"><span class="pre">max_length</span></code></a> argument.</p>
<p>The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.ClearableFileInput" title="django.forms.ClearableFileInput"><code class="xref py py-class docutils literal"><span class="pre">ClearableFileInput</span></code></a>.</p>
</div>
<div class="section" id="s-integerfield">
<span id="integerfield"></span><h3><code class="docutils literal"><span class="pre">IntegerField</span></code><a class="headerlink" href="#integerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.IntegerField">
<em class="property">class </em><code class="descname">IntegerField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#IntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.IntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An integer. Values from <code class="docutils literal"><span class="pre">-2147483648</span></code> to <code class="docutils literal"><span class="pre">2147483647</span></code> are safe in all
databases supported by Django. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.NumberInput" title="django.forms.NumberInput"><code class="xref py py-class docutils literal"><span class="pre">NumberInput</span></code></a> when <a class="reference internal" href="../forms/fields.html#django.forms.Field.localize" title="django.forms.Field.localize"><code class="xref py py-attr docutils literal"><span class="pre">localize</span></code></a>
is <code class="docutils literal"><span class="pre">False</span></code> or <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a> otherwise.</p>
</div>
<div class="section" id="s-ipaddressfield">
<span id="ipaddressfield"></span><h3><code class="docutils literal"><span class="pre">IPAddressField</span></code><a class="headerlink" href="#ipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.IPAddressField">
<em class="property">class </em><code class="descname">IPAddressField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#IPAddressField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.IPAddressField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="deprecated">
<p><span class="versionmodified">Deprecated since version 1.7: </span>This field has been deprecated in favor of
<a class="reference internal" href="#django.db.models.GenericIPAddressField" title="django.db.models.GenericIPAddressField"><code class="xref py py-class docutils literal"><span class="pre">GenericIPAddressField</span></code></a>.</p>
</div>
<p>An IP address, in string format (e.g. &#8220;192.0.2.30&#8221;). The default form widget
for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.</p>
</div>
<div class="section" id="s-genericipaddressfield">
<span id="genericipaddressfield"></span><h3><code class="docutils literal"><span class="pre">GenericIPAddressField</span></code><a class="headerlink" href="#genericipaddressfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.GenericIPAddressField">
<em class="property">class </em><code class="descname">GenericIPAddressField</code>(<em>protocol='both'</em>, <em>unpack_ipv4=False</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#GenericIPAddressField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.GenericIPAddressField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An IPv4 or IPv6 address, in string format (e.g. <code class="docutils literal"><span class="pre">192.0.2.30</span></code> or
<code class="docutils literal"><span class="pre">2a02:42fe::4</span></code>). The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.</p>
<p>The IPv6 address normalization follows <span class="target" id="index-0"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc4291.html#section-2.2"><strong>RFC 4291#section-2.2</strong></a> section 2.2,
including using the IPv4 format suggested in paragraph 3 of that section, like
<code class="docutils literal"><span class="pre">::ffff:192.0.2.0</span></code>. For example, <code class="docutils literal"><span class="pre">2001:0::0:01</span></code> would be normalized to
<code class="docutils literal"><span class="pre">2001::1</span></code>, and <code class="docutils literal"><span class="pre">::ffff:0a0a:0a0a</span></code> to <code class="docutils literal"><span class="pre">::ffff:10.10.10.10</span></code>. All characters
are converted to lowercase.</p>
<dl class="attribute">
<dt id="django.db.models.GenericIPAddressField.protocol">
<code class="descclassname">GenericIPAddressField.</code><code class="descname">protocol</code><a class="headerlink" href="#django.db.models.GenericIPAddressField.protocol" title="Permalink to this definition">¶</a></dt>
<dd><p>Limits valid inputs to the specified protocol.
Accepted values are <code class="docutils literal"><span class="pre">'both'</span></code> (default), <code class="docutils literal"><span class="pre">'IPv4'</span></code>
or <code class="docutils literal"><span class="pre">'IPv6'</span></code>. Matching is case insensitive.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.GenericIPAddressField.unpack_ipv4">
<code class="descclassname">GenericIPAddressField.</code><code class="descname">unpack_ipv4</code><a class="headerlink" href="#django.db.models.GenericIPAddressField.unpack_ipv4" title="Permalink to this definition">¶</a></dt>
<dd><p>Unpacks IPv4 mapped addresses like <code class="docutils literal"><span class="pre">::ffff:192.0.2.1</span></code>.
If this option is enabled that address would be unpacked to
<code class="docutils literal"><span class="pre">192.0.2.1</span></code>. Default is disabled. Can only be used
when <code class="docutils literal"><span class="pre">protocol</span></code> is set to <code class="docutils literal"><span class="pre">'both'</span></code>.</p>
</dd></dl>

<p>If you allow for blank values, you have to allow for null values since blank
values are stored as null.</p>
</div>
<div class="section" id="s-nullbooleanfield">
<span id="nullbooleanfield"></span><h3><code class="docutils literal"><span class="pre">NullBooleanField</span></code><a class="headerlink" href="#nullbooleanfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.NullBooleanField">
<em class="property">class </em><code class="descname">NullBooleanField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#NullBooleanField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.NullBooleanField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Like a <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><code class="xref py py-class docutils literal"><span class="pre">BooleanField</span></code></a>, but allows <code class="docutils literal"><span class="pre">NULL</span></code> as one of the options. Use
this instead of a <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><code class="xref py py-class docutils literal"><span class="pre">BooleanField</span></code></a> with <code class="docutils literal"><span class="pre">null=True</span></code>. The default form
widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.NullBooleanSelect" title="django.forms.NullBooleanSelect"><code class="xref py py-class docutils literal"><span class="pre">NullBooleanSelect</span></code></a>.</p>
</div>
<div class="section" id="s-positiveintegerfield">
<span id="positiveintegerfield"></span><h3><code class="docutils literal"><span class="pre">PositiveIntegerField</span></code><a class="headerlink" href="#positiveintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.PositiveIntegerField">
<em class="property">class </em><code class="descname">PositiveIntegerField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#PositiveIntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.PositiveIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">IntegerField</span></code></a>, but must be either positive or zero (<code class="docutils literal"><span class="pre">0</span></code>).
Values from <code class="docutils literal"><span class="pre">0</span></code> to <code class="docutils literal"><span class="pre">2147483647</span></code> are safe in all databases supported by
Django. The value <code class="docutils literal"><span class="pre">0</span></code> is accepted for backward compatibility reasons.</p>
</div>
<div class="section" id="s-positivesmallintegerfield">
<span id="positivesmallintegerfield"></span><h3><code class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></code><a class="headerlink" href="#positivesmallintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.PositiveSmallIntegerField">
<em class="property">class </em><code class="descname">PositiveSmallIntegerField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#PositiveSmallIntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.PositiveSmallIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">PositiveIntegerField</span></code></a>, but only allows values under a certain
(database-dependent) point. Values from <code class="docutils literal"><span class="pre">0</span></code> to <code class="docutils literal"><span class="pre">32767</span></code> are safe in all
databases supported by Django.</p>
</div>
<div class="section" id="s-slugfield">
<span id="slugfield"></span><h3><code class="docutils literal"><span class="pre">SlugField</span></code><a class="headerlink" href="#slugfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.SlugField">
<em class="property">class </em><code class="descname">SlugField</code>(<em>max_length=50</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#SlugField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.SlugField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p><a class="reference internal" href="../../glossary.html#term-slug"><span class="xref std std-term">Slug</span></a> is a newspaper term. A slug is a short label for something,
containing only letters, numbers, underscores or hyphens. They&#8217;re generally used
in URLs.</p>
<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"><span class="pre">max_length</span></code></a> (read the note
about 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"><span class="pre">max_length</span></code></a> in that section,
too). 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"><span class="pre">max_length</span></code></a> is not specified, Django will use a
default length of 50.</p>
<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"><span class="pre">Field.db_index</span></code></a> to <code class="docutils literal"><span class="pre">True</span></code>.</p>
<p>It is often useful to automatically prepopulate a SlugField based on the value
of some other value.  You can do this automatically in the admin using
<a class="reference internal" href="../contrib/admin/index.html#django.contrib.admin.ModelAdmin.prepopulated_fields" title="django.contrib.admin.ModelAdmin.prepopulated_fields"><code class="xref py py-attr docutils literal"><span class="pre">prepopulated_fields</span></code></a>.</p>
</div>
<div class="section" id="s-smallintegerfield">
<span id="smallintegerfield"></span><h3><code class="docutils literal"><span class="pre">SmallIntegerField</span></code><a class="headerlink" href="#smallintegerfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.SmallIntegerField">
<em class="property">class </em><code class="descname">SmallIntegerField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#SmallIntegerField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.SmallIntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<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"><span class="pre">IntegerField</span></code></a>, but only allows values under a certain
(database-dependent) point. Values from <code class="docutils literal"><span class="pre">-32768</span></code> to <code class="docutils literal"><span class="pre">32767</span></code> are safe in all
databases supported by Django.</p>
</div>
<div class="section" id="s-textfield">
<span id="textfield"></span><h3><code class="docutils literal"><span class="pre">TextField</span></code><a class="headerlink" href="#textfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.TextField">
<em class="property">class </em><code class="descname">TextField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#TextField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.TextField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A large text field. The default form widget for this field is a
<a class="reference internal" href="../forms/widgets.html#django.forms.Textarea" title="django.forms.Textarea"><code class="xref py py-class docutils literal"><span class="pre">Textarea</span></code></a>.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>If you specify a <code class="docutils literal"><span class="pre">max_length</span></code> attribute, it will be reflected in the
<a class="reference internal" href="../forms/widgets.html#django.forms.Textarea" title="django.forms.Textarea"><code class="xref py py-class docutils literal"><span class="pre">Textarea</span></code></a> widget of the auto-generated form field.
However it is not enforced at the model or database level. Use a
<a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> for that.</p>
</div>
<div class="admonition-mysql-users admonition">
<p class="first admonition-title">MySQL users</p>
<p class="last">If you are using this field with MySQLdb 1.2.1p2 and the <code class="docutils literal"><span class="pre">utf8_bin</span></code>
collation (which is <em>not</em> the default), there are some issues to be aware
of. Refer to the <a class="reference internal" href="../databases.html#mysql-collation"><span class="std std-ref">MySQL database notes</span></a> for
details.</p>
</div>
</div>
<div class="section" id="s-timefield">
<span id="timefield"></span><h3><code class="docutils literal"><span class="pre">TimeField</span></code><a class="headerlink" href="#timefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.TimeField">
<em class="property">class </em><code class="descname">TimeField</code>(<em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#TimeField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.TimeField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A time, represented in Python by a <code class="docutils literal"><span class="pre">datetime.time</span></code> instance. Accepts the same
auto-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"><span class="pre">DateField</span></code></a>.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.
The admin adds some JavaScript shortcuts.</p>
</div>
<div class="section" id="s-urlfield">
<span id="urlfield"></span><h3><code class="docutils literal"><span class="pre">URLField</span></code><a class="headerlink" href="#urlfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.URLField">
<em class="property">class </em><code class="descname">URLField</code>(<em>max_length=200</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#URLField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.URLField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> for a URL.</p>
<p>The default form widget for this field is a <a class="reference internal" href="../forms/widgets.html#django.forms.TextInput" title="django.forms.TextInput"><code class="xref py py-class docutils literal"><span class="pre">TextInput</span></code></a>.</p>
<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"><span class="pre">CharField</span></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"><span class="pre">URLField</span></code></a> takes the optional
<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"><span class="pre">max_length</span></code></a> argument. If you don&#8217;t 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"><span class="pre">max_length</span></code></a>, a default of 200 is used.</p>
</div>
<div class="section" id="s-uuidfield">
<span id="uuidfield"></span><h3><code class="docutils literal"><span class="pre">UUIDField</span></code><a class="headerlink" href="#uuidfield" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<dl class="class">
<dt id="django.db.models.UUIDField">
<em class="property">class </em><code class="descname">UUIDField</code>(<em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#UUIDField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.UUIDField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A field for storing universally unique identifiers. Uses Python&#8217;s
<code class="xref py py-class docutils literal"><span class="pre">UUID</span></code> class. When used on PostgreSQL, this stores in a
<code class="docutils literal"><span class="pre">uuid</span></code> datatype, otherwise in a <code class="docutils literal"><span class="pre">char(32)</span></code>.</p>
<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"><span class="pre">AutoField</span></code></a> for
<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"><span class="pre">primary_key</span></code></a>. The database will not generate the UUID for you, so
it 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"><span class="pre">default</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">MyUUIDModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">UUIDField</span><span class="p">(</span><span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">,</span> <span class="n">editable</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
    <span class="c1"># other fields</span>
</pre></div>
</div>
<p>Note that a callable (with the parentheses omitted) is passed to <code class="docutils literal"><span class="pre">default</span></code>,
not an instance of <code class="docutils literal"><span class="pre">UUID</span></code>.</p>
</div>
</div>
<div class="section" id="s-module-django.db.models.fields.related">
<span id="s-relationship-fields"></span><span id="module-django.db.models.fields.related"></span><span id="relationship-fields"></span><h2>Relationship fields<a class="headerlink" href="#module-django.db.models.fields.related" title="Permalink to this headline">¶</a></h2>
<p>Django also defines a set of fields that represent relations.</p>
<div class="section" id="s-foreignkey">
<span id="s-ref-foreignkey"></span><span id="foreignkey"></span><span id="ref-foreignkey"></span><h3><code class="docutils literal"><span class="pre">ForeignKey</span></code><a class="headerlink" href="#foreignkey" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ForeignKey">
<em class="property">class </em><code class="descname">ForeignKey</code>(<em>othermodel</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/related.html#ForeignKey"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.ForeignKey" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A many-to-one relationship. Requires a positional argument: the class to which
the model is related.</p>
<p id="recursive-relationships">To create a recursive relationship &#8211; an object that has a many-to-one
relationship with itself &#8211; use <code class="docutils literal"><span class="pre">models.ForeignKey('self')</span></code>.</p>
<p id="lazy-relationships">If you need to create a relationship on a model that has not yet been defined,
you can use the name of the model, rather than the model object itself:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">manufacturer</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;Manufacturer&#39;</span><span class="p">)</span>
    <span class="c1"># ...</span>

<span class="k">class</span> <span class="nc">Manufacturer</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p>To refer to models defined in another application, you can explicitly specify
a model with the full application label. For example, if the <code class="docutils literal"><span class="pre">Manufacturer</span></code>
model above is defined in another application called <code class="docutils literal"><span class="pre">production</span></code>, you&#8217;d
need to use:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Car</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">manufacturer</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;production.Manufacturer&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>This sort of reference can be useful when resolving circular import
dependencies between two applications.</p>
<p>A database index is automatically created on the <code class="docutils literal"><span class="pre">ForeignKey</span></code>. You can
disable 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"><span class="pre">db_index</span></code></a> to <code class="docutils literal"><span class="pre">False</span></code>.  You may want to
avoid the overhead of an index if you are creating a foreign key for
consistency rather than joins, or if you will be creating an alternative index
like a partial or multiple column index.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is not recommended to have a <code class="docutils literal"><span class="pre">ForeignKey</span></code> from an app without migrations
to an app with migrations. See the <a class="reference internal" href="../../topics/migrations.html#unmigrated-dependencies"><span class="std std-ref">dependencies documentation</span></a> for more details.</p>
</div>
<div class="section" id="s-database-representation">
<span id="database-representation"></span><h4>Database Representation<a class="headerlink" href="#database-representation" title="Permalink to this headline">¶</a></h4>
<p>Behind the scenes, Django appends <code class="docutils literal"><span class="pre">&quot;_id&quot;</span></code> to the field name to create its
database column name. In the above example, the database table for the <code class="docutils literal"><span class="pre">Car</span></code>
model will have a <code class="docutils literal"><span class="pre">manufacturer_id</span></code> column. (You can change this explicitly by
specifying <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"><span class="pre">db_column</span></code></a>) However, your code should never have to
deal with the database column name, unless you write custom SQL. You&#8217;ll always
deal with the field names of your model object.</p>
</div>
<div class="section" id="s-arguments">
<span id="s-foreign-key-arguments"></span><span id="arguments"></span><span id="foreign-key-arguments"></span><h4>Arguments<a class="headerlink" href="#arguments" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> accepts an extra set of arguments &#8211; all optional &#8211; that
define the details of how the relation works.</p>
<dl class="attribute">
<dt id="django.db.models.ForeignKey.limit_choices_to">
<code class="descclassname">ForeignKey.</code><code class="descname">limit_choices_to</code><a class="headerlink" href="#django.db.models.ForeignKey.limit_choices_to" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets a limit to the available choices for this field when this field is
rendered using a <code class="docutils literal"><span class="pre">ModelForm</span></code> or the admin (by default, all objects
in the queryset are available to choose). Either a dictionary, a
<a class="reference internal" href="querysets.html#django.db.models.Q" title="django.db.models.Q"><code class="xref py py-class docutils literal"><span class="pre">Q</span></code></a> object, or a callable returning a
dictionary or <a class="reference internal" href="querysets.html#django.db.models.Q" title="django.db.models.Q"><code class="xref py py-class docutils literal"><span class="pre">Q</span></code></a> object can be used.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">staff_member</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">limit_choices_to</span><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>
</pre></div>
</div>
<p>causes the corresponding field on the <code class="docutils literal"><span class="pre">ModelForm</span></code> to list only <code class="docutils literal"><span class="pre">Users</span></code>
that have <code class="docutils literal"><span class="pre">is_staff=True</span></code>. This may be helpful in the Django admin.</p>
<p>The callable form can be helpful, for instance, when used in conjunction
with the Python <code class="docutils literal"><span class="pre">datetime</span></code> module to limit selections by date range. For
example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">limit_pub_date_choices</span><span class="p">():</span>
    <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;pub_date__lte&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">utcnow</span><span class="p">()}</span>

<span class="n">limit_choices_to</span> <span class="o">=</span> <span class="n">limit_pub_date_choices</span>
</pre></div>
</div>
<p>If <code class="docutils literal"><span class="pre">limit_choices_to</span></code> is or returns a <a class="reference internal" href="querysets.html#django.db.models.Q" title="django.db.models.Q"><code class="xref py py-class docutils literal"><span class="pre">Q</span> <span class="pre">object</span></code></a>, which is useful for <a class="reference internal" href="../../topics/db/queries.html#complex-lookups-with-q"><span class="std std-ref">complex queries</span></a>, then it will only have an effect on the choices
available in the admin when the field is not listed in
<a class="reference internal" href="../contrib/admin/index.html#django.contrib.admin.ModelAdmin.raw_id_fields" title="django.contrib.admin.ModelAdmin.raw_id_fields"><code class="xref py py-attr docutils literal"><span class="pre">raw_id_fields</span></code></a> in the
<code class="docutils literal"><span class="pre">ModelAdmin</span></code> for the model.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>Previous versions of Django do not allow passing a callable as a value
for <code class="docutils literal"><span class="pre">limit_choices_to</span></code>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If a callable is used for <code class="docutils literal"><span class="pre">limit_choices_to</span></code>, it will be invoked
every time a new form is instantiated. It may also be invoked when a
model is validated, for example by management commands or the admin.
The admin constructs querysets to validate its form inputs in various
edge cases multiple times, so there is a possibility your callable may
be invoked several times.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.related_name">
<code class="descclassname">ForeignKey.</code><code class="descname">related_name</code><a class="headerlink" href="#django.db.models.ForeignKey.related_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name to use for the relation from the related object back to this one.
It&#8217;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"><span class="pre">related_query_name</span></code></a> (the name to use
for the reverse filter name from the target model). See the <a class="reference internal" href="../../topics/db/queries.html#backwards-related-objects"><span class="std std-ref">related
objects documentation</span></a> for a full explanation
and example. Note that you must set this value when defining relations on
<a class="reference internal" href="../../topics/db/models.html#abstract-base-classes"><span class="std std-ref">abstract models</span></a>; and when you do so
<a class="reference internal" href="../../topics/db/models.html#abstract-related-name"><span class="std std-ref">some special syntax</span></a> is available.</p>
<p>If you&#8217;d prefer Django not to create a backwards relation, set
<code class="docutils literal"><span class="pre">related_name</span></code> to <code class="docutils literal"><span class="pre">'+'</span></code> or end it with <code class="docutils literal"><span class="pre">'+'</span></code>. For example, this will
ensure that the <code class="docutils literal"><span class="pre">User</span></code> model won&#8217;t have a backwards relation to this
model:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.related_query_name">
<code class="descclassname">ForeignKey.</code><code class="descname">related_query_name</code><a class="headerlink" href="#django.db.models.ForeignKey.related_query_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name to use for the reverse filter name from the target model.
Defaults 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"><span class="pre">related_name</span></code></a> if it is set, otherwise it
defaults to the name of the model:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Declare the ForeignKey with related_query_name</span>
<span class="k">class</span> <span class="nc">Tag</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">article</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Article</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;tags&quot;</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="s2">&quot;tag&quot;</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">)</span>

<span class="c1"># That&#39;s now the name of the reverse filter</span>
<span class="n">Article</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">tag__name</span><span class="o">=</span><span class="s2">&quot;important&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.to_field">
<code class="descclassname">ForeignKey.</code><code class="descname">to_field</code><a class="headerlink" href="#django.db.models.ForeignKey.to_field" title="Permalink to this definition">¶</a></dt>
<dd><p>The field on the related object that the relation is to. By default, Django
uses the primary key of the related object.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.db_constraint">
<code class="descclassname">ForeignKey.</code><code class="descname">db_constraint</code><a class="headerlink" href="#django.db.models.ForeignKey.db_constraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Controls whether or not a constraint should be created in the database for
this foreign key. The default is <code class="docutils literal"><span class="pre">True</span></code>, and that&#8217;s almost certainly what
you want; setting this to <code class="docutils literal"><span class="pre">False</span></code> can be very bad for data integrity.
That said, here are some scenarios where you might want to do this:</p>
<ul class="simple">
<li>You have legacy data that is not valid.</li>
<li>You&#8217;re sharding your database.</li>
</ul>
<p>If this is set to <code class="docutils literal"><span class="pre">False</span></code>, accessing a related object that doesn&#8217;t exist
will raise its <code class="docutils literal"><span class="pre">DoesNotExist</span></code> exception.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.on_delete">
<code class="descclassname">ForeignKey.</code><code class="descname">on_delete</code><a class="headerlink" href="#django.db.models.ForeignKey.on_delete" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey</span></code></a> is deleted, Django by
default emulates the behavior of the SQL constraint <code class="docutils literal"><span class="pre">ON</span> <span class="pre">DELETE</span> <span class="pre">CASCADE</span></code>
and also deletes the object containing the <code class="docutils literal"><span class="pre">ForeignKey</span></code>. This behavior
can be overridden by specifying the <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"><span class="pre">on_delete</span></code></a> argument. For
example, if you have a nullable <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> and you want it to be
set null when the referenced object is deleted:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SET_NULL</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<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"><span class="pre">on_delete</span></code></a> are found in
<a class="reference internal" href="../../topics/db/models.html#module-django.db.models" title="django.db.models"><code class="xref py py-mod docutils literal"><span class="pre">django.db.models</span></code></a>:</p>
<ul>
<li><dl class="attribute">
<dt id="django.db.models.CASCADE">
<code class="descname">CASCADE</code><a class="reference internal" href="../../_modules/django/db/models/deletion.html#CASCADE"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.CASCADE" title="Permalink to this definition">¶</a></dt>
<dd><p>Cascade deletes; the default.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.PROTECT">
<code class="descname">PROTECT</code><a class="reference internal" href="../../_modules/django/db/models/deletion.html#PROTECT"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.PROTECT" title="Permalink to this definition">¶</a></dt>
<dd><p>Prevent deletion of the referenced object by raising
<a class="reference internal" href="../exceptions.html#django.db.models.ProtectedError" title="django.db.models.ProtectedError"><code class="xref py py-exc docutils literal"><span class="pre">ProtectedError</span></code></a>, a subclass of
<a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><code class="xref py py-exc docutils literal"><span class="pre">django.db.IntegrityError</span></code></a>.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.SET_NULL">
<code class="descname">SET_NULL</code><a class="reference internal" href="../../_modules/django/db/models/deletion.html#SET_NULL"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.SET_NULL" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey</span></code></a> null; this is only possible 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"><span class="pre">null</span></code></a> is <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.SET_DEFAULT">
<code class="descname">SET_DEFAULT</code><a class="reference internal" href="../../_modules/django/db/models/deletion.html#SET_DEFAULT"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.SET_DEFAULT" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey</span></code></a> to its default value; a default for the
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> must be set.</p>
</dd></dl>

</li>
<li><dl class="function">
<dt id="django.db.models.SET">
<code class="descname">SET</code>()<a class="reference internal" href="../../_modules/django/db/models/deletion.html#SET"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.SET" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey</span></code></a> to the value passed to
<a class="reference internal" href="#django.db.models.SET" title="django.db.models.SET"><code class="xref py py-func docutils literal"><span class="pre">SET()</span></code></a>, or if a callable is passed in,
the result of calling it. In most cases, passing a callable will be
necessary to avoid executing queries at the time your models.py is
imported:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="k">import</span> <span class="n">get_user_model</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">def</span> <span class="nf">get_sentinel_user</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">get_user_model</span><span class="p">()</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s1">&#39;deleted&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span>
                             <span class="n">on_delete</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">SET</span><span class="p">(</span><span class="n">get_sentinel_user</span><span class="p">))</span>
</pre></div>
</div>
</dd></dl>

</li>
<li><dl class="attribute">
<dt id="django.db.models.DO_NOTHING">
<code class="descname">DO_NOTHING</code><a class="reference internal" href="../../_modules/django/db/models/deletion.html#DO_NOTHING"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.DO_NOTHING" title="Permalink to this definition">¶</a></dt>
<dd><p>Take no action. If your database backend enforces referential
integrity, this will cause an <a class="reference internal" href="../exceptions.html#django.db.IntegrityError" title="django.db.IntegrityError"><code class="xref py py-exc docutils literal"><span class="pre">IntegrityError</span></code></a> unless
you manually add an SQL <code class="docutils literal"><span class="pre">ON</span> <span class="pre">DELETE</span></code> constraint to the database field
(perhaps using <a class="reference internal" href="../../howto/initial-data.html#initial-sql"><span class="std std-ref">initial sql</span></a>).</p>
</dd></dl>

</li>
</ul>
<dl class="attribute">
<dt id="django.db.models.ForeignKey.swappable">
<code class="descclassname">ForeignKey.</code><code class="descname">swappable</code><a class="headerlink" href="#django.db.models.ForeignKey.swappable" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Controls the migration framework&#8217;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"><span class="pre">ForeignKey</span></code></a>
is pointing at a swappable model. If it is <code class="docutils literal"><span class="pre">True</span></code> - the default -
then if the <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> is pointing at a model which matches
the current value of <code class="docutils literal"><span class="pre">settings.AUTH_USER_MODEL</span></code> (or another swappable
model setting) the relationship will be stored in the migration using
a reference to the setting, not to the model directly.</p>
<p>You only want to override this to be <code class="docutils literal"><span class="pre">False</span></code> if you are sure your
model should always point towards the swapped-in model - for example,
if it is a profile model designed specifically for your custom user model.</p>
<p>Setting it to <code class="docutils literal"><span class="pre">False</span></code> does not mean you can reference a swappable model
even if it is swapped out - <code class="docutils literal"><span class="pre">False</span></code> just means that the migrations made
with this ForeignKey will always reference the exact model you specify
(so it will fail hard if the user tries to run with a User model you don&#8217;t
support, for example).</p>
<p>If in doubt, leave it to its default of <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-manytomanyfield">
<span id="s-ref-manytomany"></span><span id="manytomanyfield"></span><span id="ref-manytomany"></span><h3><code class="docutils literal"><span class="pre">ManyToManyField</span></code><a class="headerlink" href="#manytomanyfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.ManyToManyField">
<em class="property">class </em><code class="descname">ManyToManyField</code>(<em>othermodel</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/related.html#ManyToManyField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.ManyToManyField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A many-to-many relationship. Requires a positional argument: the class to
which the model is related, which 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"><span class="pre">ForeignKey</span></code></a>, including <a class="reference internal" href="#recursive-relationships"><span class="std std-ref">recursive</span></a> and
<a class="reference internal" href="#lazy-relationships"><span class="std std-ref">lazy</span></a> relationships.</p>
<p>Related objects can be added, removed, or created with the field&#8217;s
<a class="reference internal" href="relations.html#django.db.models.fields.related.RelatedManager" title="django.db.models.fields.related.RelatedManager"><code class="xref py py-class docutils literal"><span class="pre">RelatedManager</span></code></a>.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">It is not recommended to have a <code class="docutils literal"><span class="pre">ManyToManyField</span></code> from an app without migrations
to an app with migrations. See the <a class="reference internal" href="../../topics/migrations.html#unmigrated-dependencies"><span class="std std-ref">dependencies documentation</span></a> for more details.</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h4>Database Representation<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h4>
<p>Behind the scenes, Django creates an intermediary join table to represent the
many-to-many relationship. By default, this table name is generated using the
name of the many-to-many field and the name of the table for the model that
contains it. Since some databases don&#8217;t support table names above a certain
length, these table names will be automatically truncated to 64 characters and a
uniqueness hash will be used. This means you might see table names like
<code class="docutils literal"><span class="pre">author_books_9cdf4</span></code>; this is perfectly normal.  You can manually provide the
name of the 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"><span class="pre">db_table</span></code></a> option.</p>
</div>
<div class="section" id="s-manytomany-arguments">
<span id="s-id2"></span><span id="manytomany-arguments"></span><span id="id2"></span><h4>Arguments<a class="headerlink" href="#manytomany-arguments" title="Permalink to this headline">¶</a></h4>
<p><a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> accepts an extra set of arguments &#8211; all optional &#8211;
that control how the relationship functions.</p>
<dl class="attribute">
<dt id="django.db.models.ManyToManyField.related_name">
<code class="descclassname">ManyToManyField.</code><code class="descname">related_name</code><a class="headerlink" href="#django.db.models.ManyToManyField.related_name" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey.related_name</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.related_query_name">
<code class="descclassname">ManyToManyField.</code><code class="descname">related_query_name</code><a class="headerlink" href="#django.db.models.ManyToManyField.related_query_name" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey.related_query_name</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.limit_choices_to">
<code class="descclassname">ManyToManyField.</code><code class="descname">limit_choices_to</code><a class="headerlink" href="#django.db.models.ManyToManyField.limit_choices_to" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">ForeignKey.limit_choices_to</span></code></a>.</p>
<p><code class="docutils literal"><span class="pre">limit_choices_to</span></code> has no effect when used on a <code class="docutils literal"><span class="pre">ManyToManyField</span></code> with a
custom intermediate table specified using 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"><span class="pre">through</span></code></a> parameter.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.symmetrical">
<code class="descclassname">ManyToManyField.</code><code class="descname">symmetrical</code><a class="headerlink" href="#django.db.models.ManyToManyField.symmetrical" title="Permalink to this definition">¶</a></dt>
<dd><p>Only used in the definition of ManyToManyFields on self. Consider the
following model:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">friends</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="s2">&quot;self&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>When Django processes this model, it identifies that it has a
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> on itself, and as a result, it doesn&#8217;t add a
<code class="docutils literal"><span class="pre">person_set</span></code> attribute to the <code class="docutils literal"><span class="pre">Person</span></code> class. Instead, the
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> is assumed to be symmetrical &#8211; that is, if I am
your friend, then you are my friend.</p>
<p>If you do not want symmetry in many-to-many relationships with <code class="docutils literal"><span class="pre">self</span></code>, set
<a class="reference internal" href="#django.db.models.ManyToManyField.symmetrical" title="django.db.models.ManyToManyField.symmetrical"><code class="xref py py-attr docutils literal"><span class="pre">symmetrical</span></code></a> to <code class="docutils literal"><span class="pre">False</span></code>. This will force Django to
add the descriptor for the reverse relationship, allowing
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> relationships to be non-symmetrical.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.through">
<code class="descclassname">ManyToManyField.</code><code class="descname">through</code><a class="headerlink" href="#django.db.models.ManyToManyField.through" title="Permalink to this definition">¶</a></dt>
<dd><p>Django will automatically generate a table to manage many-to-many
relationships. However, if you want to manually specify the intermediary
table, 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"><span class="pre">through</span></code></a> option to specify
the Django model that represents the intermediate table that you want to
use.</p>
<p>The most common use for this option is when you want to associate
<a class="reference internal" href="../../topics/db/models.html#intermediary-manytomany"><span class="std std-ref">extra data with a many-to-many relationship</span></a>.</p>
<p>If you don&#8217;t specify an explicit <code class="docutils literal"><span class="pre">through</span></code> model, there is still an
implicit <code class="docutils literal"><span class="pre">through</span></code> model class you can use to directly access the table
created to hold the association. It has three fields to link the models.</p>
<p>If the source and target models differ, the following fields are
generated:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">id</span></code>: the primary key of the relation.</li>
<li><code class="docutils literal"><span class="pre">&lt;containing_model&gt;_id</span></code>: the <code class="docutils literal"><span class="pre">id</span></code> of the model that declares the
<code class="docutils literal"><span class="pre">ManyToManyField</span></code>.</li>
<li><code class="docutils literal"><span class="pre">&lt;other_model&gt;_id</span></code>: the <code class="docutils literal"><span class="pre">id</span></code> of the model that the
<code class="docutils literal"><span class="pre">ManyToManyField</span></code> points to.</li>
</ul>
<p>If the <code class="docutils literal"><span class="pre">ManyToManyField</span></code> points from and to the same model, the following
fields are generated:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">id</span></code>: the primary key of the relation.</li>
<li><code class="docutils literal"><span class="pre">from_&lt;model&gt;_id</span></code>: the <code class="docutils literal"><span class="pre">id</span></code> of the instance which points at the
model (i.e. the source instance).</li>
<li><code class="docutils literal"><span class="pre">to_&lt;model&gt;_id</span></code>: the <code class="docutils literal"><span class="pre">id</span></code> of the instance to which the relationship
points (i.e. the target model instance).</li>
</ul>
<p>This class can be used to query associated records for a given model
instance like a normal model.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.through_fields">
<code class="descclassname">ManyToManyField.</code><code class="descname">through_fields</code><a class="headerlink" href="#django.db.models.ManyToManyField.through_fields" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Only used when a custom intermediary model is specified. Django will
normally determine which fields of the intermediary model to use in order
to establish a many-to-many relationship automatically. However,
consider the following models:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Group</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
    <span class="n">members</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ManyToManyField</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">through</span><span class="o">=</span><span class="s1">&#39;Membership&#39;</span><span class="p">,</span> <span class="n">through_fields</span><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>

<span class="k">class</span> <span class="nc">Membership</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">group</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Group</span><span class="p">)</span>
    <span class="n">person</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">)</span>
    <span class="n">inviter</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Person</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s2">&quot;membership_invites&quot;</span><span class="p">)</span>
    <span class="n">invite_reason</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">64</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">Membership</span></code> has <em>two</em> foreign keys to <code class="docutils literal"><span class="pre">Person</span></code> (<code class="docutils literal"><span class="pre">person</span></code> and
<code class="docutils literal"><span class="pre">inviter</span></code>), which makes the relationship ambiguous and Django can&#8217;t know
which one to use. In this case, you must explicitly specify which
foreign keys Django should use using <code class="docutils literal"><span class="pre">through_fields</span></code>, as in the example
above.</p>
<p><code class="docutils literal"><span class="pre">through_fields</span></code> accepts a 2-tuple <code class="docutils literal"><span class="pre">('field1',</span> <span class="pre">'field2')</span></code>, where
<code class="docutils literal"><span class="pre">field1</span></code> is the name of the foreign key to the model the
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> is defined on (<code class="docutils literal"><span class="pre">group</span></code> in this case), and
<code class="docutils literal"><span class="pre">field2</span></code> the name of the foreign key to the target model (<code class="docutils literal"><span class="pre">person</span></code>
in this case).</p>
<p>When you have more than one foreign key on an intermediary model to any
(or even both) of the models participating in a many-to-many relationship,
you <em>must</em> specify <code class="docutils literal"><span class="pre">through_fields</span></code>. This also applies to
<a class="reference internal" href="#recursive-relationships"><span class="std std-ref">recursive relationships</span></a>
when an intermediary model is used and there are more than two
foreign keys to the model, or you want to explicitly specify which two
Django should use.</p>
<p>Recursive relationships using an intermediary model are always defined as
non-symmetrical &#8211; 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"><span class="pre">symmetrical=False</span></code></a>
&#8211; therefore, there is the concept of a &#8220;source&#8221; and a &#8220;target&#8221;. In that
case <code class="docutils literal"><span class="pre">'field1'</span></code> will be treated as the &#8220;source&#8221; of the relationship and
<code class="docutils literal"><span class="pre">'field2'</span></code> as the &#8220;target&#8221;.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.db_table">
<code class="descclassname">ManyToManyField.</code><code class="descname">db_table</code><a class="headerlink" href="#django.db.models.ManyToManyField.db_table" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the table to create for storing the many-to-many data. If this
is not provided, Django will assume a default name based upon the names of:
the table for the model defining the relationship and the name of the field
itself.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.db_constraint">
<code class="descclassname">ManyToManyField.</code><code class="descname">db_constraint</code><a class="headerlink" href="#django.db.models.ManyToManyField.db_constraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Controls whether or not constraints should be created in the database for
the foreign keys in the intermediary table. The default is <code class="docutils literal"><span class="pre">True</span></code>, and
that&#8217;s almost certainly what you want; setting this to <code class="docutils literal"><span class="pre">False</span></code> can be
very bad for data integrity. That said, here are some scenarios where you
might want to do this:</p>
<ul class="simple">
<li>You have legacy data that is not valid.</li>
<li>You&#8217;re sharding your database.</li>
</ul>
<p>It is an error to pass both <code class="docutils literal"><span class="pre">db_constraint</span></code> and <code class="docutils literal"><span class="pre">through</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.swappable">
<code class="descclassname">ManyToManyField.</code><code class="descname">swappable</code><a class="headerlink" href="#django.db.models.ManyToManyField.swappable" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Controls the migration framework&#8217;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"><span class="pre">ManyToManyField</span></code></a>
is pointing at a swappable model. If it is <code class="docutils literal"><span class="pre">True</span></code> - the default -
then if the <a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> is pointing at a model which matches
the current value of <code class="docutils literal"><span class="pre">settings.AUTH_USER_MODEL</span></code> (or another swappable
model setting) the relationship will be stored in the migration using
a reference to the setting, not to the model directly.</p>
<p>You only want to override this to be <code class="docutils literal"><span class="pre">False</span></code> if you are sure your
model should always point towards the swapped-in model - for example,
if it is a profile model designed specifically for your custom user model.</p>
<p>If in doubt, leave it to its default of <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>

<p><a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></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"><span class="pre">validators</span></code></a>.</p>
<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"><span class="pre">null</span></code></a> has no effect since there is no way to require a
relationship at the database level.</p>
</div>
</div>
<div class="section" id="s-onetoonefield">
<span id="s-ref-onetoone"></span><span id="onetoonefield"></span><span id="ref-onetoone"></span><h3><code class="docutils literal"><span class="pre">OneToOneField</span></code><a class="headerlink" href="#onetoonefield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.OneToOneField">
<em class="property">class </em><code class="descname">OneToOneField</code>(<em>othermodel</em>, <em>parent_link=False</em>, <em>**options</em>)<a class="reference internal" href="../../_modules/django/db/models/fields/related.html#OneToOneField"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.OneToOneField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A one-to-one relationship. Conceptually, this is similar to a
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></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"><span class="pre">unique=True</span></code></a>, but the
&#8220;reverse&#8221; side of the relation will directly return a single object.</p>
<p>This is most useful as the primary key of a model which &#8220;extends&#8221;
another model in some way; <a class="reference internal" href="../../topics/db/models.html#multi-table-inheritance"><span class="std std-ref">Multi-table inheritance</span></a> is
implemented by adding an implicit one-to-one relation from the child
model to the parent model, for example.</p>
<p>One positional argument is required: the class to which the model will be
related. 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"><span class="pre">ForeignKey</span></code></a>,
including all the options regarding <a class="reference internal" href="#recursive-relationships"><span class="std std-ref">recursive</span></a>
and <a class="reference internal" href="#lazy-relationships"><span class="std std-ref">lazy</span></a> relationships.</p>
<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"><span class="pre">related_name</span></code></a> argument for
the <code class="docutils literal"><span class="pre">OneToOneField</span></code>, Django will use the lower-case name of the current model
as default value.</p>
<p>With the following example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">MySpecialUser</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">)</span>
    <span class="n">supervisor</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">OneToOneField</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s1">&#39;supervisor_of&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>your resulting <code class="docutils literal"><span class="pre">User</span></code> model will have the following attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">user</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="s1">&#39;myspecialuser&#39;</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="s1">&#39;supervisor_of&#39;</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>A <code class="docutils literal"><span class="pre">DoesNotExist</span></code> exception is raised when accessing the reverse relationship
if an entry in the related table doesn&#8217;t exist. For example, if a user doesn&#8217;t
have a supervisor designated by <code class="docutils literal"><span class="pre">MySpecialUser</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">user</span><span class="o">.</span><span class="n">supervisor_of</span>
<span class="gt">Traceback (most recent call last):</span>
    <span class="o">...</span>
<span class="gr">DoesNotExist</span>: <span class="n">User matching query does not exist.</span>
</pre></div>
</div>
<p id="onetoone-arguments">Additionally, <code class="docutils literal"><span class="pre">OneToOneField</span></code> accepts all of the extra arguments
accepted by <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a>, plus one extra argument:</p>
<dl class="attribute">
<dt id="django.db.models.OneToOneField.parent_link">
<code class="descclassname">OneToOneField.</code><code class="descname">parent_link</code><a class="headerlink" href="#django.db.models.OneToOneField.parent_link" title="Permalink to this definition">¶</a></dt>
<dd><p>When <code class="docutils literal"><span class="pre">True</span></code> and used in a model which inherits from another
<a class="reference internal" href="../../glossary.html#term-concrete-model"><span class="xref std std-term">concrete model</span></a>, indicates that this field should be used as the
link back to the parent class, rather than the extra
<code class="docutils literal"><span class="pre">OneToOneField</span></code> which would normally be implicitly created by
subclassing.</p>
</dd></dl>

<p>See <a class="reference internal" href="../../topics/db/examples/one_to_one.html"><span class="doc">One-to-one relationships</span></a> for usage
examples of <code class="docutils literal"><span class="pre">OneToOneField</span></code>.</p>
</div>
</div>
<div class="section" id="s-field-api-reference">
<span id="field-api-reference"></span><h2>Field API reference<a class="headerlink" href="#field-api-reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.db.models.Field">
<em class="property">class </em><code class="descname">Field</code><a class="reference internal" href="../../_modules/django/db/models/fields.html#Field"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal"><span class="pre">Field</span></code> is an abstract class that represents a database table column.
Django 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"><span class="pre">db_type()</span></code></a>), to map
Python 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"><span class="pre">get_prep_value()</span></code></a>) and vice-versa
(<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"><span class="pre">from_db_value()</span></code></a>), and to apply <a class="reference internal" href="lookups.html"><span class="doc">Lookup API reference</span></a>
(<a class="reference internal" href="#django.db.models.Field.get_prep_lookup" title="django.db.models.Field.get_prep_lookup"><code class="xref py py-meth docutils literal"><span class="pre">get_prep_lookup()</span></code></a>).</p>
<p>A field is thus a fundamental piece in different Django APIs, notably,
<a class="reference internal" href="instances.html#django.db.models.Model" title="django.db.models.Model"><code class="xref py py-class docutils literal"><span class="pre">models</span></code></a> and <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal"><span class="pre">querysets</span></code></a>.</p>
<p>In models, a field is instantiated as a class attribute and represents a
particular table column, see <a class="reference internal" href="../../topics/db/models.html"><span class="doc">Models</span></a>. It has attributes
such 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"><span class="pre">null</span></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"><span class="pre">unique</span></code></a>, and methods that Django uses to
map the field value to database-specific values.</p>
<p>A <code class="docutils literal"><span class="pre">Field</span></code> is a subclass of
<a class="reference internal" href="lookups.html#django.db.models.lookups.RegisterLookupMixin" title="django.db.models.lookups.RegisterLookupMixin"><code class="xref py py-class docutils literal"><span class="pre">RegisterLookupMixin</span></code></a> and thus both
<a class="reference internal" href="lookups.html#django.db.models.Transform" title="django.db.models.Transform"><code class="xref py py-class docutils literal"><span class="pre">Transform</span></code></a> and
<a class="reference internal" href="lookups.html#django.db.models.Lookup" title="django.db.models.Lookup"><code class="xref py py-class docutils literal"><span class="pre">Lookup</span></code></a> can be registered on it to be used
in <code class="docutils literal"><span class="pre">QuerySet</span></code>s (e.g. <code class="docutils literal"><span class="pre">field_name__exact=&quot;foo&quot;</span></code>). All <a class="reference internal" href="querysets.html#field-lookups"><span class="std std-ref">built-in
lookups</span></a> are registered by default.</p>
<p>All of Django&#8217;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"><span class="pre">CharField</span></code></a>, are particular
implementations of <code class="docutils literal"><span class="pre">Field</span></code>. If you need a custom field, you can either
subclass any of the built-in fields or write a <code class="docutils literal"><span class="pre">Field</span></code> from scratch. In
either case, see <a class="reference internal" href="../../howto/custom-model-fields.html"><span class="doc">Writing custom model fields</span></a>.</p>
<dl class="attribute">
<dt id="django.db.models.Field.description">
<code class="descname">description</code><a class="headerlink" href="#django.db.models.Field.description" title="Permalink to this definition">¶</a></dt>
<dd><p>A verbose description of the field, e.g. for the
<a class="reference internal" href="../contrib/admin/admindocs.html#module-django.contrib.admindocs" title="django.contrib.admindocs: Django's admin documentation generator."><code class="xref py py-mod docutils literal"><span class="pre">django.contrib.admindocs</span></code></a> application.</p>
<p>The description can be of the form:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">description</span> <span class="o">=</span> <span class="n">_</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>
</pre></div>
</div>
<p>where the arguments are interpolated from the field&#8217;s <code class="docutils literal"><span class="pre">__dict__</span></code>.</p>
</dd></dl>

<p>To map a <code class="docutils literal"><span class="pre">Field</span></code> to a database-specific type, Django exposes two methods:</p>
<dl class="method">
<dt id="django.db.models.Field.get_internal_type">
<code class="descname">get_internal_type</code>()<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_internal_type"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_internal_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string naming this field for backend specific purposes.
By default, it returns the class name.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#emulating-built-in-field-types"><span class="std std-ref">Emulating built-in field types</span></a> for usage in custom fields.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.db_type">
<code class="descname">db_type</code>(<em>connection</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.db_type"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.db_type" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">Field</span></code></a>, taking
into account the <code class="docutils literal"><span class="pre">connection</span></code>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#custom-database-types"><span class="std std-ref">Custom database types</span></a> for usage in custom fields.</p>
</dd></dl>

<p>There are three main situations where Django needs to interact with the
database backend and fields:</p>
<ul class="simple">
<li>when it queries the database (Python value -&gt; database backend value)</li>
<li>when it loads data from the database (database backend value -&gt; Python
value)</li>
<li>when it saves to the database (Python value -&gt; database backend value)</li>
</ul>
<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"><span class="pre">get_db_prep_value()</span></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"><span class="pre">get_prep_value()</span></code></a> are used:</p>
<dl class="method">
<dt id="django.db.models.Field.get_prep_value">
<code class="descname">get_prep_value</code>(<em>value</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_prep_value"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_prep_value" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal"><span class="pre">value</span></code> is the current value of the model&#8217;s attribute, and the method
should return data in a format that has been prepared for use as a
parameter in a query.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-python-objects-to-query-values"><span class="std std-ref">Converting Python objects to query values</span></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.get_db_prep_value">
<code class="descname">get_db_prep_value</code>(<em>value</em>, <em>connection</em>, <em>prepared=False</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_db_prep_value"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_db_prep_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts <code class="docutils literal"><span class="pre">value</span></code> to a backend-specific value. By default it returns
<code class="docutils literal"><span class="pre">value</span></code> if <code class="docutils literal"><span class="pre">prepared=True</span></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"><span class="pre">get_prep_value()</span></code></a> if is
<code class="docutils literal"><span class="pre">False</span></code>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-query-values-to-database-values"><span class="std std-ref">Converting query values to database values</span></a> for usage.</p>
</dd></dl>

<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"><span class="pre">from_db_value()</span></code></a> is used:</p>
<dl class="method">
<dt id="django.db.models.Field.from_db_value">
<code class="descname">from_db_value</code>(<em>value</em>, <em>expression</em>, <em>connection</em>, <em>context</em>)<a class="headerlink" href="#django.db.models.Field.from_db_value" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>Converts a value as returned by the database to a Python object. It is
the 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"><span class="pre">get_prep_value()</span></code></a>.</p>
<p>This method is not used for most built-in fields as the database
backend already returns the correct Python type, or the backend itself
does the conversion.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-values-to-python-objects"><span class="std std-ref">Converting values to Python objects</span></a> for usage.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For performance reasons, <code class="docutils literal"><span class="pre">from_db_value</span></code> is not implemented as a
no-op on fields which do not require it (all Django fields).
Consequently you may not call <code class="docutils literal"><span class="pre">super</span></code> in your definition.</p>
</div>
</dd></dl>

<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"><span class="pre">pre_save()</span></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"><span class="pre">get_db_prep_save()</span></code></a> are used:</p>
<dl class="method">
<dt id="django.db.models.Field.get_db_prep_save">
<code class="descname">get_db_prep_save</code>(<em>value</em>, <em>connection</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_db_prep_save"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_db_prep_save" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">get_db_prep_value()</span></code></a>, but called when the field value
must be <em>saved</em> to the database. By default returns
<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"><span class="pre">get_db_prep_value()</span></code></a>.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.pre_save">
<code class="descname">pre_save</code>(<em>model_instance</em>, <em>add</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.pre_save"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.pre_save" title="Permalink to this definition">¶</a></dt>
<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"><span class="pre">get_db_prep_save()</span></code></a> to prepare the value
before 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"><span class="pre">DateField.auto_now</span></code></a>).</p>
<p><code class="docutils literal"><span class="pre">model_instance</span></code> is the instance this field belongs to and <code class="docutils literal"><span class="pre">add</span></code>
is whether the instance is being saved to the database for the first
time.</p>
<p>It should return the value of the appropriate attribute from
<code class="docutils literal"><span class="pre">model_instance</span></code> for this field. The attribute name is in
<code class="docutils literal"><span class="pre">self.attname</span></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"><span class="pre">Field</span></code></a>).</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#preprocessing-values-before-saving"><span class="std std-ref">Preprocessing values before saving</span></a> for usage.</p>
</dd></dl>

<p>When a lookup is used on a field, the value may need to be &#8220;prepared&#8221;.
Django exposes two methods for this:</p>
<dl class="method">
<dt id="django.db.models.Field.get_prep_lookup">
<code class="descname">get_prep_lookup</code>(<em>lookup_type</em>, <em>value</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_prep_lookup"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_prep_lookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Prepares <code class="docutils literal"><span class="pre">value</span></code> to the database prior to be used in a lookup.
The <code class="docutils literal"><span class="pre">lookup_type</span></code> will be one of the valid Django filter lookups:
<code class="docutils literal"><span class="pre">&quot;exact&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;iexact&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;contains&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;icontains&quot;</span></code>,
<code class="docutils literal"><span class="pre">&quot;gt&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;gte&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;lt&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;lte&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;in&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;startswith&quot;</span></code>,
<code class="docutils literal"><span class="pre">&quot;istartswith&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;endswith&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;iendswith&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;range&quot;</span></code>,
<code class="docutils literal"><span class="pre">&quot;year&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;month&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;day&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;isnull&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;search&quot;</span></code>,
<code class="docutils literal"><span class="pre">&quot;regex&quot;</span></code>, and <code class="docutils literal"><span class="pre">&quot;iregex&quot;</span></code>.</p>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>If you are using <a class="reference internal" href="lookups.html"><span class="doc">Custom lookups</span></a> the
<code class="docutils literal"><span class="pre">lookup_type</span></code> can be any <code class="docutils literal"><span class="pre">lookup_name</span></code> registered in the field.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#preparing-values-for-use-in-database-lookups"><span class="std std-ref">Preparing values for use in database lookups</span></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.get_db_prep_lookup">
<code class="descname">get_db_prep_lookup</code>(<em>lookup_type</em>, <em>value</em>, <em>connection</em>, <em>prepared=False</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.get_db_prep_lookup"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.get_db_prep_lookup" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to <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"><span class="pre">get_db_prep_value()</span></code></a>, but for performing a lookup.</p>
<p>As with <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"><span class="pre">get_db_prep_value()</span></code></a>, the specific connection that will
be used for the query is passed as <code class="docutils literal"><span class="pre">connection</span></code>. In addition,
<code class="docutils literal"><span class="pre">prepared</span></code> describes whether the value has already been prepared with
<a class="reference internal" href="#django.db.models.Field.get_prep_lookup" title="django.db.models.Field.get_prep_lookup"><code class="xref py py-meth docutils literal"><span class="pre">get_prep_lookup()</span></code></a>.</p>
</dd></dl>

<p>Fields often receive their values as a different type, either from
serialization or from forms.</p>
<dl class="method">
<dt id="django.db.models.Field.to_python">
<code class="descname">to_python</code>(<em>value</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.to_python"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.to_python" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts the value into the correct Python object. It acts as the
reverse 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"><span class="pre">value_to_string()</span></code></a>, and is also called in
<a class="reference internal" href="instances.html#django.db.models.Model.clean" title="django.db.models.Model.clean"><code class="xref py py-meth docutils literal"><span class="pre">clean()</span></code></a>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-values-to-python-objects"><span class="std std-ref">Converting values to Python objects</span></a> for usage.</p>
</dd></dl>

<p>Besides saving to the database, the field also needs to know how to
serialize its value:</p>
<dl class="method">
<dt id="django.db.models.Field.value_to_string">
<code class="descname">value_to_string</code>(<em>obj</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.value_to_string"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.value_to_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts <code class="docutils literal"><span class="pre">obj</span></code> to a string. Used to serialize the value of the field.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#converting-model-field-to-serialization"><span class="std std-ref">Converting field data for serialization</span></a> for usage.</p>
</dd></dl>

<p>When using <a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal"><span class="pre">model</span> <span class="pre">forms</span></code></a>, the <code class="docutils literal"><span class="pre">Field</span></code>
needs to know which form field it should be represented by:</p>
<dl class="method">
<dt id="django.db.models.Field.formfield">
<code class="descname">formfield</code>(<em>form_class=None</em>, <em>choices_form_class=None</em>, <em>**kwargs</em>)<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.formfield"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.formfield" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the default <a class="reference internal" href="../forms/fields.html#django.forms.Field" title="django.forms.Field"><code class="xref py py-class docutils literal"><span class="pre">django.forms.Field</span></code></a> of this field for
<a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal"><span class="pre">ModelForm</span></code></a>.</p>
<p>By default, if both <code class="docutils literal"><span class="pre">form_class</span></code> and <code class="docutils literal"><span class="pre">choices_form_class</span></code> are
<code class="docutils literal"><span class="pre">None</span></code>, it uses <a class="reference internal" href="../forms/fields.html#django.forms.CharField" title="django.forms.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a>; if
<code class="docutils literal"><span class="pre">choices_form_class</span></code> is given, it returns
<a class="reference internal" href="../forms/fields.html#django.forms.TypedChoiceField" title="django.forms.TypedChoiceField"><code class="xref py py-class docutils literal"><span class="pre">TypedChoiceField</span></code></a>.</p>
<p>See <a class="reference internal" href="../../howto/custom-model-fields.html#specifying-form-field-for-model-field"><span class="std std-ref">Specifying the form field for a model field</span></a> for usage.</p>
</dd></dl>

<dl class="method">
<dt id="django.db.models.Field.deconstruct">
<code class="descname">deconstruct</code>()<a class="reference internal" href="../../_modules/django/db/models/fields.html#Field.deconstruct"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.Field.deconstruct" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Returns a 4-tuple with enough information to recreate the field:</p>
<ol class="arabic simple">
<li>The name of the field on the model.</li>
<li>The import path of the field (e.g. <code class="docutils literal"><span class="pre">&quot;django.db.models.IntegerField&quot;</span></code>).
This should be the most portable version, so less specific may be better.</li>
<li>A list of positional arguments.</li>
<li>A dict of keyword arguments.</li>
</ol>
<p>This method must be added to fields prior to 1.7 to migrate its data
using <a class="reference internal" href="../../topics/migrations.html"><span class="doc">Migrations</span></a>.</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="s-field-attribute-reference">
<span id="s-model-field-attributes"></span><span id="field-attribute-reference"></span><span id="model-field-attributes"></span><h1>Field attribute reference<a class="headerlink" href="#field-attribute-reference" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>Every <code class="docutils literal"><span class="pre">Field</span></code> instance contains several attributes that allow
introspecting its behavior. Use these attributes instead of <code class="docutils literal"><span class="pre">isinstance</span></code>
checks when you need to write code that depends on a field&#8217;s functionality.
These attributes can be used together with the <a class="reference internal" href="meta.html#model-meta-field-api"><span class="std std-ref">Model._meta API</span></a> to narrow down a search for specific field types.
Custom model fields should implement these flags.</p>
<div class="section" id="s-attributes-for-fields">
<span id="attributes-for-fields"></span><h2>Attributes for fields<a class="headerlink" href="#attributes-for-fields" title="Permalink to this headline">¶</a></h2>
<dl class="attribute">
<dt id="django.db.models.Field.auto_created">
<code class="descclassname">Field.</code><code class="descname">auto_created</code><a class="headerlink" href="#django.db.models.Field.auto_created" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that indicates if the field was automatically created, such
as the <code class="docutils literal"><span class="pre">OneToOneField</span></code> used by model inheritance.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.concrete">
<code class="descclassname">Field.</code><code class="descname">concrete</code><a class="headerlink" href="#django.db.models.Field.concrete" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that indicates if the field has a database column associated
with it.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.hidden">
<code class="descclassname">Field.</code><code class="descname">hidden</code><a class="headerlink" href="#django.db.models.Field.hidden" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that indicates if a field is used to back another non-hidden
field&#8217;s functionality (e.g. the <code class="docutils literal"><span class="pre">content_type</span></code> and <code class="docutils literal"><span class="pre">object_id</span></code> fields
that make up a <code class="docutils literal"><span class="pre">GenericForeignKey</span></code>). The <code class="docutils literal"><span class="pre">hidden</span></code> flag is used to
distinguish what constitutes the public subset of fields on the model from
all the fields on the model.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="meta.html#django.db.models.options.Options.get_fields" title="django.db.models.options.Options.get_fields"><code class="xref py py-meth docutils literal"><span class="pre">Options.get_fields()</span></code></a>
excludes hidden fields by default. Pass in <code class="docutils literal"><span class="pre">include_hidden=True</span></code> to
return hidden fields in the results.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.is_relation">
<code class="descclassname">Field.</code><code class="descname">is_relation</code><a class="headerlink" href="#django.db.models.Field.is_relation" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that indicates if a field contains references to one or
more other models for its functionality (e.g. <code class="docutils literal"><span class="pre">ForeignKey</span></code>,
<code class="docutils literal"><span class="pre">ManyToManyField</span></code>, <code class="docutils literal"><span class="pre">OneToOneField</span></code>, etc.).</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.model">
<code class="descclassname">Field.</code><code class="descname">model</code><a class="headerlink" href="#django.db.models.Field.model" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the model on which the field is defined. If a field is defined on
a superclass of a model, <code class="docutils literal"><span class="pre">model</span></code> will refer to the superclass, not the
class of the instance.</p>
</dd></dl>

</div>
<div class="section" id="s-attributes-for-fields-with-relations">
<span id="attributes-for-fields-with-relations"></span><h2>Attributes for fields with relations<a class="headerlink" href="#attributes-for-fields-with-relations" title="Permalink to this headline">¶</a></h2>
<p>These attributes are used to query for the cardinality and other details of a
relation. These attribute are present on all fields; however, they will only
have boolean values (rather than <code class="docutils literal"><span class="pre">None</span></code>) if the field is a relation type
(<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"><span class="pre">Field.is_relation=True</span></code></a>).</p>
<dl class="attribute">
<dt id="django.db.models.Field.many_to_many">
<code class="descclassname">Field.</code><code class="descname">many_to_many</code><a class="headerlink" href="#django.db.models.Field.many_to_many" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that is <code class="docutils literal"><span class="pre">True</span></code> if the field has a many-to-many relation;
<code class="docutils literal"><span class="pre">False</span></code> otherwise. The only field included with Django where this is
<code class="docutils literal"><span class="pre">True</span></code> is <code class="docutils literal"><span class="pre">ManyToManyField</span></code>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.many_to_one">
<code class="descclassname">Field.</code><code class="descname">many_to_one</code><a class="headerlink" href="#django.db.models.Field.many_to_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that is <code class="docutils literal"><span class="pre">True</span></code> if the field has a many-to-one relation, such
as a <code class="docutils literal"><span class="pre">ForeignKey</span></code>; <code class="docutils literal"><span class="pre">False</span></code> otherwise.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.one_to_many">
<code class="descclassname">Field.</code><code class="descname">one_to_many</code><a class="headerlink" href="#django.db.models.Field.one_to_many" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that is <code class="docutils literal"><span class="pre">True</span></code> if the field has a one-to-many relation, such
as a <code class="docutils literal"><span class="pre">GenericRelation</span></code> or the reverse of a <code class="docutils literal"><span class="pre">ForeignKey</span></code>; <code class="docutils literal"><span class="pre">False</span></code>
otherwise.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.one_to_one">
<code class="descclassname">Field.</code><code class="descname">one_to_one</code><a class="headerlink" href="#django.db.models.Field.one_to_one" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean flag that is <code class="docutils literal"><span class="pre">True</span></code> if the field has a one-to-one relation, such
as a <code class="docutils literal"><span class="pre">OneToOneField</span></code>; <code class="docutils literal"><span class="pre">False</span></code> otherwise.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.Field.related_model">
<code class="descclassname">Field.</code><code class="descname">related_model</code><a class="headerlink" href="#django.db.models.Field.related_model" title="Permalink to this definition">¶</a></dt>
<dd><p>Points to the model the field relates to. For example, <code class="docutils literal"><span class="pre">Author</span></code> in
<code class="docutils literal"><span class="pre">ForeignKey(Author)</span></code>. If a field has a generic relation (such as a
<code class="docutils literal"><span class="pre">GenericForeignKey</span></code> or a <code class="docutils literal"><span class="pre">GenericRelation</span></code>) then <code class="docutils literal"><span class="pre">related_model</span></code>
will be <code class="docutils literal"><span class="pre">None</span></code>.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Model field reference</a><ul>
<li><a class="reference internal" href="#field-options">Field options</a><ul>
<li><a class="reference internal" href="#null"><code class="docutils literal"><span class="pre">null</span></code></a></li>
<li><a class="reference internal" href="#blank"><code class="docutils literal"><span class="pre">blank</span></code></a></li>
<li><a class="reference internal" href="#choices"><code class="docutils literal"><span class="pre">choices</span></code></a></li>
<li><a class="reference internal" href="#db-column"><code class="docutils literal"><span class="pre">db_column</span></code></a></li>
<li><a class="reference internal" href="#db-index"><code class="docutils literal"><span class="pre">db_index</span></code></a></li>
<li><a class="reference internal" href="#db-tablespace"><code class="docutils literal"><span class="pre">db_tablespace</span></code></a></li>
<li><a class="reference internal" href="#default"><code class="docutils literal"><span class="pre">default</span></code></a></li>
<li><a class="reference internal" href="#editable"><code class="docutils literal"><span class="pre">editable</span></code></a></li>
<li><a class="reference internal" href="#error-messages"><code class="docutils literal"><span class="pre">error_messages</span></code></a></li>
<li><a class="reference internal" href="#help-text"><code class="docutils literal"><span class="pre">help_text</span></code></a></li>
<li><a class="reference internal" href="#primary-key"><code class="docutils literal"><span class="pre">primary_key</span></code></a></li>
<li><a class="reference internal" href="#unique"><code class="docutils literal"><span class="pre">unique</span></code></a></li>
<li><a class="reference internal" href="#unique-for-date"><code class="docutils literal"><span class="pre">unique_for_date</span></code></a></li>
<li><a class="reference internal" href="#unique-for-month"><code class="docutils literal"><span class="pre">unique_for_month</span></code></a></li>
<li><a class="reference internal" href="#unique-for-year"><code class="docutils literal"><span class="pre">unique_for_year</span></code></a></li>
<li><a class="reference internal" href="#verbose-name"><code class="docutils literal"><span class="pre">verbose_name</span></code></a></li>
<li><a class="reference internal" href="#validators"><code class="docutils literal"><span class="pre">validators</span></code></a><ul>
<li><a class="reference internal" href="#registering-and-fetching-lookups">Registering and fetching lookups</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#field-types">Field types</a><ul>
<li><a class="reference internal" href="#autofield"><code class="docutils literal"><span class="pre">AutoField</span></code></a></li>
<li><a class="reference internal" href="#bigintegerfield"><code class="docutils literal"><span class="pre">BigIntegerField</span></code></a></li>
<li><a class="reference internal" href="#binaryfield"><code class="docutils literal"><span class="pre">BinaryField</span></code></a></li>
<li><a class="reference internal" href="#booleanfield"><code class="docutils literal"><span class="pre">BooleanField</span></code></a></li>
<li><a class="reference internal" href="#charfield"><code class="docutils literal"><span class="pre">CharField</span></code></a></li>
<li><a class="reference internal" href="#commaseparatedintegerfield"><code class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></code></a></li>
<li><a class="reference internal" href="#datefield"><code class="docutils literal"><span class="pre">DateField</span></code></a></li>
<li><a class="reference internal" href="#datetimefield"><code class="docutils literal"><span class="pre">DateTimeField</span></code></a></li>
<li><a class="reference internal" href="#decimalfield"><code class="docutils literal"><span class="pre">DecimalField</span></code></a></li>
<li><a class="reference internal" href="#durationfield"><code class="docutils literal"><span class="pre">DurationField</span></code></a></li>
<li><a class="reference internal" href="#emailfield"><code class="docutils literal"><span class="pre">EmailField</span></code></a></li>
<li><a class="reference internal" href="#filefield"><code class="docutils literal"><span class="pre">FileField</span></code></a><ul>
<li><a class="reference internal" href="#filefield-and-fieldfile">FileField and FieldFile</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filepathfield"><code class="docutils literal"><span class="pre">FilePathField</span></code></a></li>
<li><a class="reference internal" href="#floatfield"><code class="docutils literal"><span class="pre">FloatField</span></code></a></li>
<li><a class="reference internal" href="#imagefield"><code class="docutils literal"><span class="pre">ImageField</span></code></a></li>
<li><a class="reference internal" href="#integerfield"><code class="docutils literal"><span class="pre">IntegerField</span></code></a></li>
<li><a class="reference internal" href="#ipaddressfield"><code class="docutils literal"><span class="pre">IPAddressField</span></code></a></li>
<li><a class="reference internal" href="#genericipaddressfield"><code class="docutils literal"><span class="pre">GenericIPAddressField</span></code></a></li>
<li><a class="reference internal" href="#nullbooleanfield"><code class="docutils literal"><span class="pre">NullBooleanField</span></code></a></li>
<li><a class="reference internal" href="#positiveintegerfield"><code class="docutils literal"><span class="pre">PositiveIntegerField</span></code></a></li>
<li><a class="reference internal" href="#positivesmallintegerfield"><code class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></code></a></li>
<li><a class="reference internal" href="#slugfield"><code class="docutils literal"><span class="pre">SlugField</span></code></a></li>
<li><a class="reference internal" href="#smallintegerfield"><code class="docutils literal"><span class="pre">SmallIntegerField</span></code></a></li>
<li><a class="reference internal" href="#textfield"><code class="docutils literal"><span class="pre">TextField</span></code></a></li>
<li><a class="reference internal" href="#timefield"><code class="docutils literal"><span class="pre">TimeField</span></code></a></li>
<li><a class="reference internal" href="#urlfield"><code class="docutils literal"><span class="pre">URLField</span></code></a></li>
<li><a class="reference internal" href="#uuidfield"><code class="docutils literal"><span class="pre">UUIDField</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#module-django.db.models.fields.related">Relationship fields</a><ul>
<li><a class="reference internal" href="#foreignkey"><code class="docutils literal"><span class="pre">ForeignKey</span></code></a><ul>
<li><a class="reference internal" href="#database-representation">Database Representation</a></li>
<li><a class="reference internal" href="#arguments">Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#manytomanyfield"><code class="docutils literal"><span class="pre">ManyToManyField</span></code></a><ul>
<li><a class="reference internal" href="#id1">Database Representation</a></li>
<li><a class="reference internal" href="#manytomany-arguments">Arguments</a></li>
</ul>
</li>
<li><a class="reference internal" href="#onetoonefield"><code class="docutils literal"><span class="pre">OneToOneField</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#field-api-reference">Field API reference</a></li>
</ul>
</li>
<li><a class="reference internal" href="#field-attribute-reference">Field attribute reference</a><ul>
<li><a class="reference internal" href="#attributes-for-fields">Attributes for fields</a></li>
<li><a class="reference internal" href="#attributes-for-fields-with-relations">Attributes for fields with relations</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Models</a></li>
    
    
      <li>Next: <a href="meta.html">Model <code class="docutils literal"><span class="pre">_meta</span></code> API</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.8.19 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html">Models</a>
        
        <ul><li>Model field reference</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/fields.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="Models">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="meta.html" title="Model &lt;code class=&#34;docutils literal&#34;&gt;&lt;span class=&#34;pre&#34;&gt;_meta&lt;/span&gt;&lt;/code&gt; API">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>