Sophie

Sophie

distrib > Arklinux > devel > i586 > media > main > by-pkgid > 5fcb1fedf34660bc240dc59b7bfcebc4 > files > 390

django-doc-1.2.3-1ark.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">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Model field reference &mdash; Django v1.2 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.2',
        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="top" title="Django v1.2 documentation" href="../../index.html" />
    <link rel="up" title="Models" href="index.html" />
    <link rel="next" title="Related objects reference" href="relations.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>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django v1.2 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="relations.html" title="Related objects reference">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 gory details about all the <a class="reference internal" href="#field-options">field options</a> and
<a class="reference internal" href="#field-types">field types</a> Django&#8217;s got to offer.</p>
<div class="admonition-see-also 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 easily <a class="reference internal" href="../../howto/custom-model-fields.html"><em>write your
own custom model fields</em></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."><tt class="xref py py-mod docutils literal"><span class="pre">django.db.models.fields</span></tt></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"><tt class="xref py py-mod docutils literal"><span class="pre">django.db.models</span></tt></a>; the standard
convention is to use <tt class="docutils literal"><span class="pre">from</span> <span class="pre">django.db</span> <span class="pre">import</span> <span class="pre">models</span></tt> and refer to fields as
<tt class="docutils literal"><span class="pre">models.&lt;Foo&gt;Field</span></tt>.</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><tt class="docutils literal"><span class="pre">null</span></tt><a class="headerlink" href="#null" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.null">
<tt class="descclassname">Field.</tt><tt class="descname">null</tt><a class="headerlink" href="#django.db.models.Field.null" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

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

<p>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, the field is allowed to be blank. Default is <tt class="xref docutils literal"><span class="pre">False</span></tt>.</p>
<p>Note that this is different than <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a>. <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null</span></tt></a> is
purely database-related, whereas <a class="reference internal" href="#django.db.models.Field.blank" title="django.db.models.Field.blank"><tt class="xref py py-attr docutils literal"><span class="pre">blank</span></tt></a> is validation-related. If
a field has <tt class="docutils literal"><span class="pre">blank=True</span></tt>, validation on Django&#8217;s admin site will allow entry
of an empty value. If a field has <tt class="docutils literal"><span class="pre">blank=False</span></tt>, the field will be required.</p>
</div>
<div class="section" id="s-choices">
<span id="choices"></span><h3><tt class="docutils literal"><span class="pre">choices</span></tt><a class="headerlink" href="#choices" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.choices">
<tt class="descclassname">Field.</tt><tt class="descname">choices</tt><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) of 2-tuples to use as choices for this
field.</p>
<p>If this is given, Django&#8217;s admin will use a select box instead of the standard
text field and will limit choices to the choices given.</p>
<p>A choices list looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">YEAR_IN_SCHOOL_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">&#39;FR&#39;</span><span class="p">,</span> <span class="s">&#39;Freshman&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SO&#39;</span><span class="p">,</span> <span class="s">&#39;Sophomore&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;JR&#39;</span><span class="p">,</span> <span class="s">&#39;Junior&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;SR&#39;</span><span class="p">,</span> <span class="s">&#39;Senior&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;GR&#39;</span><span class="p">,</span> <span class="s">&#39;Graduate&#39;</span><span class="p">),</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The first element in each tuple is the actual value to be stored. The second
element is the human-readable name for the option.</p>
<p>The choices list can be defined either as part of your model class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Foo</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">GENDER_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
        <span class="p">(</span><span class="s">&#39;M&#39;</span><span class="p">,</span> <span class="s">&#39;Male&#39;</span><span class="p">),</span>
        <span class="p">(</span><span class="s">&#39;F&#39;</span><span class="p">,</span> <span class="s">&#39;Female&#39;</span><span class="p">),</span>
    <span class="p">)</span>
    <span class="n">gender</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">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">GENDER_CHOICES</span><span class="p">)</span>
</pre></div>
</div>
<p>or outside your model class altogether:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">GENDER_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">&#39;M&#39;</span><span class="p">,</span> <span class="s">&#39;Male&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;F&#39;</span><span class="p">,</span> <span class="s">&#39;Female&#39;</span><span class="p">),</span>
<span class="p">)</span>
<span class="k">class</span> <span class="nc">Foo</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">gender</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">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">GENDER_CHOICES</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also collect your available choices into named groups that can
be used for organizational purposes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">MEDIA_CHOICES</span> <span class="o">=</span> <span class="p">(</span>
    <span class="p">(</span><span class="s">&#39;Audio&#39;</span><span class="p">,</span> <span class="p">(</span>
            <span class="p">(</span><span class="s">&#39;vinyl&#39;</span><span class="p">,</span> <span class="s">&#39;Vinyl&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&#39;cd&#39;</span><span class="p">,</span> <span class="s">&#39;CD&#39;</span><span class="p">),</span>
        <span class="p">)</span>
    <span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;Video&#39;</span><span class="p">,</span> <span class="p">(</span>
            <span class="p">(</span><span class="s">&#39;vhs&#39;</span><span class="p">,</span> <span class="s">&#39;VHS Tape&#39;</span><span class="p">),</span>
            <span class="p">(</span><span class="s">&#39;dvd&#39;</span><span class="p">,</span> <span class="s">&#39;DVD&#39;</span><span class="p">),</span>
        <span class="p">)</span>
    <span class="p">),</span>
    <span class="p">(</span><span class="s">&#39;unknown&#39;</span><span class="p">,</span> <span class="s">&#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"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> set, Django will add a
method to retrieve the human-readable name for the field'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"><tt class="xref py py-meth docutils literal"><span class="pre">get_FOO_display()</span></tt></a> in the database API
documentation.</p>
<p>Finally, note that choices can be any iterable object -- 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"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> to be dynamic, you'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"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>. <a class="reference internal" href="#django.db.models.Field.choices" title="django.db.models.Field.choices"><tt class="xref py py-attr docutils literal"><span class="pre">choices</span></tt></a> is
meant for static data that doesn't change much, if ever.</p>
</div>
<div class="section" id="s-db-column">
<span id="db-column"></span><h3><tt class="docutils literal"><span class="pre">db_column</span></tt><a class="headerlink" href="#db-column" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_column">
<tt class="descclassname">Field.</tt><tt class="descname">db_column</tt><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't given,
Django will use the field's name.</p>
<p>If your database column name is an SQL reserved word, or contains
characters that aren't allowed in Python variable names -- notably, the
hyphen -- that'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><tt class="docutils literal"><span class="pre">db_index</span></tt><a class="headerlink" href="#db-index" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.db_index">
<tt class="descclassname">Field.</tt><tt class="descname">db_index</tt><a class="headerlink" href="#django.db.models.Field.db_index" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

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

<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>The name of the database tablespace to use for this field's index, if this field
is indexed. The default is the project's <a class="reference internal" href="../settings.html#std:setting-DEFAULT_INDEX_TABLESPACE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_INDEX_TABLESPACE</span></tt></a>
setting, if set, or the <a class="reference internal" href="#django.db.models.Field.db_tablespace" title="django.db.models.Field.db_tablespace"><tt class="xref py py-attr docutils literal"><span class="pre">db_tablespace</span></tt></a> of the model, if any. If
the backend doesn't support tablespaces, this option is ignored.</p>
</div>
<div class="section" id="s-default">
<span id="default"></span><h3><tt class="docutils literal"><span class="pre">default</span></tt><a class="headerlink" href="#default" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.default">
<tt class="descclassname">Field.</tt><tt class="descname">default</tt><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>
</div>
<div class="section" id="s-editable">
<span id="editable"></span><h3><tt class="docutils literal"><span class="pre">editable</span></tt><a class="headerlink" href="#editable" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.editable">
<tt class="descclassname">Field.</tt><tt class="descname">editable</tt><a class="headerlink" href="#django.db.models.Field.editable" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="xref docutils literal"><span class="pre">False</span></tt>, the field will not be editable in the admin or via forms
automatically generated from the model class. Default is <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
</div>
<div class="section" id="s-error-messages">
<span id="error-messages"></span><h3><tt class="docutils literal"><span class="pre">error_messages</span></tt><a class="headerlink" href="#error-messages" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div>
<dl class="attribute">
<dt id="django.db.models.Field.error_messages">
<tt class="descclassname">Field.</tt><tt class="descname">error_messages</tt><a class="headerlink" href="#django.db.models.Field.error_messages" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The <tt class="docutils literal"><span class="pre">error_messages</span></tt> 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>
</div>
<div class="section" id="s-help-text">
<span id="help-text"></span><h3><tt class="docutils literal"><span class="pre">help_text</span></tt><a class="headerlink" href="#help-text" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.help_text">
<tt class="descclassname">Field.</tt><tt class="descname">help_text</tt><a class="headerlink" href="#django.db.models.Field.help_text" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Extra &quot;help&quot; text to be displayed under the field on the object's admin form.
It's useful for documentation even if your object doesn't have an admin form.</p>
<p>Note that this value is <em>not</em> HTML-escaped when it's displayed in the admin
interface. This lets you include HTML in <a class="reference internal" href="#django.db.models.Field.help_text" title="django.db.models.Field.help_text"><tt class="xref py py-attr docutils literal"><span class="pre">help_text</span></tt></a> if you so
desire. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">help_text</span><span class="o">=</span><span class="s">&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
<tt class="docutils literal"><span class="pre">django.utils.html.escape()</span></tt> to escape any HTML special characters.</p>
</div>
<div class="section" id="s-primary-key">
<span id="primary-key"></span><h3><tt class="docutils literal"><span class="pre">primary_key</span></tt><a class="headerlink" href="#primary-key" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.primary_key">
<tt class="descclassname">Field.</tt><tt class="descname">primary_key</tt><a class="headerlink" href="#django.db.models.Field.primary_key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, this field is the primary key for the model.</p>
<p>If you don't specify <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> for any fields in your model, Django
will automatically add an <a class="reference internal" href="#django.db.models.IntegerField" title="django.db.models.IntegerField"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> to hold the primary key, so you
don't need to set <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> 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"><em>Automatic primary key fields</em></a>.</p>
<p><tt class="docutils literal"><span class="pre">primary_key=True</span></tt> implies <a class="reference internal" href="#django.db.models.Field.null" title="django.db.models.Field.null"><tt class="xref py py-attr docutils literal"><span class="pre">null=False</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique=True</span></tt></a>.
Only one primary key is allowed on an object.</p>
</div>
<div class="section" id="s-unique">
<span id="unique"></span><h3><tt class="docutils literal"><span class="pre">unique</span></tt><a class="headerlink" href="#unique" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.unique">
<tt class="descclassname">Field.</tt><tt class="descname">unique</tt><a class="headerlink" href="#django.db.models.Field.unique" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>If <tt class="xref docutils literal"><span class="pre">True</span></tt>, this field must be unique throughout the table.</p>
<p>This is enforced at the database level and at the Django admin-form level. 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"><tt class="xref py py-attr docutils literal"><span class="pre">unique</span></tt></a>
field, a <tt class="xref py py-exc docutils literal"><span class="pre">django.db.IntegrityError</span></tt> will be raised by the model's
<a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> and
<a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>.</p>
</div>
<div class="section" id="s-unique-for-date">
<span id="unique-for-date"></span><h3><tt class="docutils literal"><span class="pre">unique_for_date</span></tt><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">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_date</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a> or <a class="reference internal" href="#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><tt class="xref py py-class docutils literal"><span class="pre">DateTimeField</span></tt></a> to
require that this field be unique for the value of the date field.</p>
<p>For example, if you have a field <tt class="docutils literal"><span class="pre">title</span></tt> that has
<tt class="docutils literal"><span class="pre">unique_for_date=&quot;pub_date&quot;</span></tt>, then Django wouldn't allow the entry of two
records with the same <tt class="docutils literal"><span class="pre">title</span></tt> and <tt class="docutils literal"><span class="pre">pub_date</span></tt>.</p>
<p>This is enforced at the Django admin-form level but not at the database level.</p>
</div>
<div class="section" id="s-unique-for-month">
<span id="unique-for-month"></span><h3><tt class="docutils literal"><span class="pre">unique_for_month</span></tt><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">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_month</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_date</span></tt></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><tt class="docutils literal"><span class="pre">unique_for_year</span></tt><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">
<tt class="descclassname">Field.</tt><tt class="descname">unique_for_year</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_date</span></tt></a> and <a class="reference internal" href="#django.db.models.Field.unique_for_month" title="django.db.models.Field.unique_for_month"><tt class="xref py py-attr docutils literal"><span class="pre">unique_for_month</span></tt></a>.</p>
</div>
<div class="section" id="s-verbose-name">
<span id="verbose-name"></span><h3><tt class="docutils literal"><span class="pre">verbose_name</span></tt><a class="headerlink" href="#verbose-name" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.Field.verbose_name">
<tt class="descclassname">Field.</tt><tt class="descname">verbose_name</tt><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't given, Django
will automatically create it using the field's attribute name, converting
underscores to spaces. See <a class="reference internal" href="../../topics/db/models.html#verbose-field-names"><em>Verbose field names</em></a>.</p>
</div>
<div class="section" id="s-validators">
<span id="validators"></span><h3><tt class="docutils literal"><span class="pre">validators</span></tt><a class="headerlink" href="#validators" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div>
<dl class="attribute">
<dt id="django.db.models.Field.validators">
<tt class="descclassname">Field.</tt><tt class="descname">validators</tt><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"><em>validators
documentation</em></a> for more information.</p>
</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><tt class="docutils literal"><span class="pre">AutoField</span></tt><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><tt class="descname">AutoField</tt>(<em>**options</em>)<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"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> that automatically increments
according to available IDs. You usually won't need to use this directly; a
primary key field will automatically be added to your model if you don't specify
otherwise. See <a class="reference internal" href="../../topics/db/models.html#automatic-primary-key-fields"><em>Automatic primary key fields</em></a>.</p>
</div>
<div class="section" id="s-bigintegerfield">
<span id="bigintegerfield"></span><h3><tt class="docutils literal"><span class="pre">BigIntegerField</span></tt><a class="headerlink" href="#bigintegerfield" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div>
<dl class="class">
<dt id="django.db.models.BigIntegerField">
<em class="property">class </em><tt class="descname">BigIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a> except that it is
guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807. The
admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
</div>
<div class="section" id="s-booleanfield">
<span id="booleanfield"></span><h3><tt class="docutils literal"><span class="pre">BooleanField</span></tt><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><tt class="descname">BooleanField</tt>(<em>**options</em>)<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 admin represents this as a checkbox.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.2:</span> <a class="reference internal" href="../../releases/1.2.html"><em>Please, see the release notes</em></a></div>
</div>
<div class="section" id="s-charfield">
<span id="charfield"></span><h3><tt class="docutils literal"><span class="pre">CharField</span></tt><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><tt class="descname">CharField</tt>(<em>max_length=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">TextField</span></tt></a>.</p>
<p>The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
<p><a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> has one extra required argument:</p>
<dl class="attribute">
<dt id="django.db.models.CharField.max_length">
<tt class="descclassname">CharField.</tt><tt class="descname">max_length</tt><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'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
<tt class="docutils literal"><span class="pre">max_length</span></tt> for some backends. Refer to the <a class="reference internal" href="../databases.html"><em>database backend
notes</em></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 <tt class="docutils literal"><span class="pre">utf8_bin</span></tt>
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"><em>MySQL database notes</em></a> for
details.</p>
</div>
</div>
<div class="section" id="s-commaseparatedintegerfield">
<span id="commaseparatedintegerfield"></span><h3><tt class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></tt><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><tt class="descname">CommaSeparatedIntegerField</tt>(<em>max_length=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a>, the
<a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></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><tt class="docutils literal"><span class="pre">DateField</span></tt><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><tt class="descname">DateField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<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 <tt class="docutils literal"><span class="pre">datetime.date</span></tt> instance. Has a few extra,
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DateField.auto_now">
<tt class="descclassname">DateField.</tt><tt class="descname">auto_now</tt><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 &quot;last-modified&quot; timestamps. Note that the current date is <em>always</em>
used; it'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">
<tt class="descclassname">DateField.</tt><tt class="descname">auto_now_add</tt><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's not just a default value that you can override.</p>
</dd></dl>

<p>The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> with a JavaScript
calendar, and a shortcut for &quot;Today&quot;.  The JavaScript calendar will always
start the week on a Sunday.</p>
</div>
<div class="section" id="s-datetimefield">
<span id="datetimefield"></span><h3><tt class="docutils literal"><span class="pre">DateTimeField</span></tt><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><tt class="descname">DateTimeField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<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 <tt class="docutils literal"><span class="pre">datetime.datetime</span></tt> instance.
Takes the same extra arguments as <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a>.</p>
<p>The admin represents this as two <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> fields, with
JavaScript shortcuts.</p>
</div>
<div class="section" id="s-decimalfield">
<span id="decimalfield"></span><h3><tt class="docutils literal"><span class="pre">DecimalField</span></tt><a class="headerlink" href="#decimalfield" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<dl class="class">
<dt id="django.db.models.DecimalField">
<em class="property">class </em><tt class="descname">DecimalField</tt>(<em>max_digits=None</em>, <em>decimal_places=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<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
<tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> instance. Has two <strong>required</strong> arguments:</p>
<dl class="attribute">
<dt id="django.db.models.DecimalField.max_digits">
<tt class="descclassname">DecimalField.</tt><tt class="descname">max_digits</tt><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</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.DecimalField.decimal_places">
<tt class="descclassname">DecimalField.</tt><tt class="descname">decimal_places</tt><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 999 with a resolution of 2 decimal places,
you'd use:</p>
<div class="highlight-python"><div class="highlight"><pre><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-python"><div class="highlight"><pre><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 admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
</div>
<div class="section" id="s-emailfield">
<span id="emailfield"></span><h3><tt class="docutils literal"><span class="pre">EmailField</span></tt><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><tt class="descname">EmailField</tt>(<span class="optional">[</span><em>max_length=75</em>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> that checks that the value is a valid e-mail address.</p>
</div>
<div class="section" id="s-filefield">
<span id="filefield"></span><h3><tt class="docutils literal"><span class="pre">FileField</span></tt><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><tt class="descname">FileField</tt>(<em>upload_to=None</em><span class="optional">[</span>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<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 <tt class="docutils literal"><span class="pre">primary_key</span></tt> and <tt class="docutils literal"><span class="pre">unique</span></tt> arguments are not supported, and will
raise a <tt class="docutils literal"><span class="pre">TypeError</span></tt> if used.</p>
</div>
<p>Has one <strong>required</strong> argument:</p>
<dl class="attribute">
<dt id="django.db.models.FileField.upload_to">
<tt class="descclassname">FileField.</tt><tt class="descname">upload_to</tt><a class="headerlink" href="#django.db.models.FileField.upload_to" title="Permalink to this definition">¶</a></dt>
<dd><p>A local filesystem path that will be appended to your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a>
setting to determine the value of the <a class="reference internal" href="../files/file.html#django.core.files.File.url" title="django.core.files.File.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a>
attribute.</p>
<p>This path may contain <a class="reference external" href="http://docs.python.org/library/time.html#time.strftime">strftime formatting</a>, which will be replaced by the
date/time of the file upload (so that uploaded files don't fill up the given
directory).</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<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><th class="head">Argument</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">instance</span></tt></td>
<td><p class="first">An instance of the model where the
<tt class="docutils literal"><span class="pre">FileField</span></tt> 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 <tt class="docutils literal"><span class="pre">AutoField</span></tt>, <em>it might not yet have a
value for its primary key field</em>.</p>
</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">filename</span></tt></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>

<p>Also has one optional argument:</p>
<dl class="attribute">
<dt id="django.db.models.FileField.storage">
<tt class="descclassname">FileField.</tt><tt class="descname">storage</tt><a class="headerlink" href="#django.db.models.FileField.storage" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>Optional. A storage object, which handles the storage and retrieval of your
files. See <a class="reference internal" href="../../topics/files.html"><em>Managing files</em></a> for details on how to provide this object.</p>
</dd></dl>

<p>The admin represents this field as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;file&quot;&gt;</span></tt> (a file-upload
widget).</p>
<p>Using a <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> or an <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> (see below) in a model
takes a few steps:</p>
<ol class="arabic simple">
<li>In your settings file, you'll need to define <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> as the
full path to a directory where you'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"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_URL</span></tt></a> as the base public URL of that directory. Make sure
that this directory is writable by the Web server's user account.</li>
<li>Add the <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> or <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> to your model, making
sure to define the <a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> option to tell Django
to which subdirectory of <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> it should upload 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"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a>). You'll most likely want to use the
convenience <a class="reference internal" href="../files/file.html#django.core.files.File.url" title="django.core.files.File.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a> function provided by
Django. For example, if your <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> is called <tt class="docutils literal"><span class="pre">mug_shot</span></tt>,
you can get the absolute URL to your image in a template with
<tt class="docutils literal"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></tt>.</li>
</ol>
<p>For example, say your <a class="reference internal" href="../settings.html#std:setting-MEDIA_ROOT"><tt class="xref std std-setting docutils literal"><span class="pre">MEDIA_ROOT</span></tt></a> is set to <tt class="docutils literal"><span class="pre">'/home/media'</span></tt>, and
<a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> is set to <tt class="docutils literal"><span class="pre">'photos/%Y/%m/%d'</span></tt>. The <tt class="docutils literal"><span class="pre">'%Y/%m/%d'</span></tt>
part of <a class="reference internal" href="#django.db.models.FileField.upload_to" title="django.db.models.FileField.upload_to"><tt class="xref py py-attr docutils literal"><span class="pre">upload_to</span></tt></a> is <a class="reference external" href="http://docs.python.org/library/time.html#time.strftime">strftime formatting</a>; <tt class="docutils literal"><span class="pre">'%Y'</span></tt> is the
four-digit year, <tt class="docutils literal"><span class="pre">'%m'</span></tt> is the two-digit month and <tt class="docutils literal"><span class="pre">'%d'</span></tt> is the two-digit
day. If you upload a file on Jan. 15, 2007, it will be saved in the directory
<tt class="docutils literal"><span class="pre">/home/media/photos/2007/01/15</span></tt>.</p>
<p>If you want to retrieve the upload file's on-disk filename, or a URL that refers
to that file, or the file's size, you can use the
<a class="reference internal" href="../files/file.html#django.core.files.File.name" title="django.core.files.File.name"><tt class="xref py py-attr docutils literal"><span class="pre">name</span></tt></a>, <a class="reference internal" href="../files/file.html#django.core.files.File.url" title="django.core.files.File.url"><tt class="xref py py-attr docutils literal"><span class="pre">url</span></tt></a>
and <a class="reference internal" href="../files/file.html#django.core.files.File.size" title="django.core.files.File.size"><tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt></a> attributes; see <a class="reference internal" href="../../topics/files.html"><em>Managing files</em></a>.</p>
<p>Note that whenever you deal with uploaded files, you should pay close attention
to where you're uploading them and what type of files they are, to avoid
security holes. <em>Validate all uploaded files</em> so that you'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's within your Web server's document
root, then somebody could upload a CGI or PHP script and execute that script by
visiting its URL on your site. Don't allow that.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> The <tt class="docutils literal"><span class="pre">max_length</span></tt> argument was added in this version.</div>
<p>By default, <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> instances are
created as <tt class="docutils literal"><span class="pre">varchar(100)</span></tt> columns in your database. 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"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></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>
<p>When you access a <a class="reference internal" href="#django.db.models.FileField" title="django.db.models.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a> on a model, you are given an instance
of <tt class="xref py py-class docutils literal"><span class="pre">FieldFile</span></tt> as a proxy for accessing the underlying file. This
class has several methods that can be used to interact with file data:</p>
<dl class="method">
<dt id="django.db.models.FieldFile.open">
<tt class="descclassname">FieldFile.</tt><tt class="descname">open</tt>(<em>mode='rb'</em>)<a class="headerlink" href="#django.db.models.FieldFile.open" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <tt class="docutils literal"><span class="pre">open()</span></tt> method and opens the file
associated with this instance in the mode specified by <tt class="docutils literal"><span class="pre">mode</span></tt>.</p>
<dl class="method">
<dt id="django.db.models.FieldFile.close">
<tt class="descclassname">FieldFile.</tt><tt class="descname">close</tt>()<a class="headerlink" href="#django.db.models.FieldFile.close" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Behaves like the standard Python <tt class="docutils literal"><span class="pre">file.close()</span></tt> method and closes the file
associated with this instance.</p>
<dl class="method">
<dt id="django.db.models.FieldFile.save">
<tt class="descclassname">FieldFile.</tt><tt class="descname">save</tt>(<em>name</em>, <em>content</em>, <em>save=True</em>)<a class="headerlink" href="#django.db.models.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"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>
instances on your model, the <tt class="docutils literal"><span class="pre">save()</span></tt> method is used to persist that file
data.</p>
<p>Takes two required arguments: <tt class="docutils literal"><span class="pre">name</span></tt> which is the name of the file, and
<tt class="docutils literal"><span class="pre">content</span></tt> which is a file-like object containing the file's contents. The
optional <tt class="docutils literal"><span class="pre">save</span></tt> argument controls whether or not the instance is saved after
the file has been altered. Defaults to <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
<dl class="method">
<dt id="django.db.models.FieldFile.delete">
<tt class="descclassname">FieldFile.</tt><tt class="descname">delete</tt>(<em>save=True</em>)<a class="headerlink" href="#django.db.models.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
<tt class="docutils literal"><span class="pre">delete()</span></tt> is called.</p>
<p>The optional <tt class="docutils literal"><span class="pre">save</span></tt> argument controls whether or not the instance is saved
after the file has been deleted. Defaults to <tt class="xref docutils literal"><span class="pre">True</span></tt>.</p>
</div>
</div>
<div class="section" id="s-filepathfield">
<span id="filepathfield"></span><h3><tt class="docutils literal"><span class="pre">FilePathField</span></tt><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><tt class="descname">FilePathField</tt>(<em>path=None</em><span class="optional">[</span>, <em>match=None</em>, <em>recursive=False</em>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></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">
<tt class="descclassname">FilePathField.</tt><tt class="descname">path</tt><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"><tt class="xref py py-class docutils literal"><span class="pre">FilePathField</span></tt></a> should get its choices. Example: <tt class="docutils literal"><span class="pre">&quot;/home/images&quot;</span></tt>.</p>
</dd></dl>

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

<dl class="attribute">
<dt id="django.db.models.FilePathField.recursive">
<tt class="descclassname">FilePathField.</tt><tt class="descname">recursive</tt><a class="headerlink" href="#django.db.models.FilePathField.recursive" title="Permalink to this definition">¶</a></dt>
<dd><p>Optional. Either <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>. Default is <tt class="xref docutils literal"><span class="pre">False</span></tt>. Specifies
whether all subdirectories of <a class="reference internal" href="#django.db.models.FilePathField.path" title="django.db.models.FilePathField.path"><tt class="xref py py-attr docutils literal"><span class="pre">path</span></tt></a> should be included</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"><tt class="xref py py-attr docutils literal"><span class="pre">match</span></tt></a> applies to the
base filename, not the full path. So, this example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">FilePathField</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="s">&quot;/home/images&quot;</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s">&quot;foo.*&quot;</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>...will match <tt class="docutils literal"><span class="pre">/home/images/foo.gif</span></tt> but not <tt class="docutils literal"><span class="pre">/home/images/foo/bar.gif</span></tt>
because the <a class="reference internal" href="#django.db.models.FilePathField.match" title="django.db.models.FilePathField.match"><tt class="xref py py-attr docutils literal"><span class="pre">match</span></tt></a> applies to the base filename
(<tt class="docutils literal"><span class="pre">foo.gif</span></tt> and <tt class="docutils literal"><span class="pre">bar.gif</span></tt>).</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> The <tt class="docutils literal"><span class="pre">max_length</span></tt> argument was added in this version.</div>
<p>By default, <a class="reference internal" href="#django.db.models.FilePathField" title="django.db.models.FilePathField"><tt class="xref py py-class docutils literal"><span class="pre">FilePathField</span></tt></a> instances are
created as <tt class="docutils literal"><span class="pre">varchar(100)</span></tt> columns in your database. 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"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument.</p>
</div>
<div class="section" id="s-floatfield">
<span id="floatfield"></span><h3><tt class="docutils literal"><span class="pre">FloatField</span></tt><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><tt class="descname">FloatField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.FloatField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="versionchanged">
<span class="title">Changed in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></div>
<p>A floating-point number represented in Python by a <tt class="docutils literal"><span class="pre">float</span></tt> instance.</p>
<p>The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
</div>
<div class="section" id="s-imagefield">
<span id="imagefield"></span><h3><tt class="docutils literal"><span class="pre">ImageField</span></tt><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><tt class="descname">ImageField</tt>(<em>upload_to=None</em><span class="optional">[</span>, <em>height_field=None</em>, <em>width_field=None</em>, <em>max_length=100</em>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>,
an <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> also has <a class="reference internal" href="../files/file.html#django.core.files.File.height" title="django.core.files.File.height"><tt class="xref py py-attr docutils literal"><span class="pre">height</span></tt></a> and
<a class="reference internal" href="../files/file.html#django.core.files.File.width" title="django.core.files.File.width"><tt class="xref py py-attr docutils literal"><span class="pre">width</span></tt></a> attributes.</p>
<p>To facilitate querying on those attributes, <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> has two extra
optional arguments:</p>
<dl class="attribute">
<dt id="django.db.models.ImageField.height_field">
<tt class="descclassname">ImageField.</tt><tt class="descname">height_field</tt><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">
<tt class="descclassname">ImageField.</tt><tt class="descname">width_field</tt><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://www.pythonware.com/products/pil/">Python Imaging Library</a>.</p>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> The <tt class="docutils literal"><span class="pre">max_length</span></tt> argument was added in this version.</div>
<p>By default, <a class="reference internal" href="#django.db.models.ImageField" title="django.db.models.ImageField"><tt class="xref py py-class docutils literal"><span class="pre">ImageField</span></tt></a> instances are created as <tt class="docutils literal"><span class="pre">varchar(100)</span></tt>
columns in your database. 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"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> argument.</p>
</div>
<div class="section" id="s-integerfield">
<span id="integerfield"></span><h3><tt class="docutils literal"><span class="pre">IntegerField</span></tt><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><tt class="descname">IntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.IntegerField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An integer. The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a
single-line input).</p>
</div>
<div class="section" id="s-ipaddressfield">
<span id="ipaddressfield"></span><h3><tt class="docutils literal"><span class="pre">IPAddressField</span></tt><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><tt class="descname">IPAddressField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.IPAddressField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>An IP address, in string format (e.g. &quot;192.0.2.30&quot;). The admin represents this
as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
</div>
<div class="section" id="s-nullbooleanfield">
<span id="nullbooleanfield"></span><h3><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt><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><tt class="descname">NullBooleanField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a>, but allows <tt class="docutils literal"><span class="pre">NULL</span></tt> as one of the options. Use
this instead of a <a class="reference internal" href="#django.db.models.BooleanField" title="django.db.models.BooleanField"><tt class="xref py py-class docutils literal"><span class="pre">BooleanField</span></tt></a> with <tt class="docutils literal"><span class="pre">null=True</span></tt>. The admin represents
this as a <tt class="docutils literal"><span class="pre">&lt;select&gt;</span></tt> box with &quot;Unknown&quot;, &quot;Yes&quot; and &quot;No&quot; choices.</p>
</div>
<div class="section" id="s-positiveintegerfield">
<span id="positiveintegerfield"></span><h3><tt class="docutils literal"><span class="pre">PositiveIntegerField</span></tt><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><tt class="descname">PositiveIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a>, but must be positive.</p>
</div>
<div class="section" id="s-positivesmallintegerfield">
<span id="positivesmallintegerfield"></span><h3><tt class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></tt><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><tt class="descname">PositiveSmallIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">PositiveIntegerField</span></tt></a>, but only allows values under a certain
(database-dependent) point.</p>
</div>
<div class="section" id="s-slugfield">
<span id="slugfield"></span><h3><tt class="docutils literal"><span class="pre">SlugField</span></tt><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><tt class="descname">SlugField</tt>(<span class="optional">[</span><em>max_length=50</em>, <em>**options</em><span class="optional">]</span>)<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"><em class="xref std std-term">Slug</em></a> is a newspaper term. A slug is a short label for something,
containing only letters, numbers, underscores or hyphens. They'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"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></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"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></a> in that section,
too). If <a class="reference internal" href="#django.db.models.CharField.max_length" title="django.db.models.CharField.max_length"><tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt></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"><tt class="xref py py-attr docutils literal"><span class="pre">Field.db_index</span></tt></a> to <tt class="xref docutils literal"><span class="pre">True</span></tt>.</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"><tt class="xref py py-attr docutils literal"><span class="pre">prepopulated_fields</span></tt></a>.</p>
</div>
<div class="section" id="s-smallintegerfield">
<span id="smallintegerfield"></span><h3><tt class="docutils literal"><span class="pre">SmallIntegerField</span></tt><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><tt class="descname">SmallIntegerField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">IntegerField</span></tt></a>, but only allows values under a certain
(database-dependent) point.</p>
</div>
<div class="section" id="s-textfield">
<span id="textfield"></span><h3><tt class="docutils literal"><span class="pre">TextField</span></tt><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><tt class="descname">TextField</tt>(<span class="optional">[</span><em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.TextField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A large text field. The admin represents this as a <tt class="docutils literal"><span class="pre">&lt;textarea&gt;</span></tt> (a multi-line
input).</p>
<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 <tt class="docutils literal"><span class="pre">utf8_bin</span></tt>
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"><em>MySQL database notes</em></a> for
details.</p>
</div>
</div>
<div class="section" id="s-timefield">
<span id="timefield"></span><h3><tt class="docutils literal"><span class="pre">TimeField</span></tt><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><tt class="descname">TimeField</tt>(<span class="optional">[</span><em>auto_now=False</em>, <em>auto_now_add=False</em>, <em>**options</em><span class="optional">]</span>)<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 <tt class="docutils literal"><span class="pre">datetime.time</span></tt> instance. Accepts the same
auto-population options as <a class="reference internal" href="#django.db.models.DateField" title="django.db.models.DateField"><tt class="xref py py-class docutils literal"><span class="pre">DateField</span></tt></a>.</p>
<p>The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> with some JavaScript
shortcuts.</p>
</div>
<div class="section" id="s-urlfield">
<span id="urlfield"></span><h3><tt class="docutils literal"><span class="pre">URLField</span></tt><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><tt class="descname">URLField</tt>(<span class="optional">[</span><em>verify_exists=True</em>, <em>max_length=200</em>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> for a URL. Has one extra optional argument:</p>
<dl class="attribute">
<dt id="django.db.models.URLField.verify_exists">
<tt class="descclassname">URLField.</tt><tt class="descname">verify_exists</tt><a class="headerlink" href="#django.db.models.URLField.verify_exists" title="Permalink to this definition">¶</a></dt>
<dd><p>If <tt class="xref docutils literal"><span class="pre">True</span></tt> (the default), the URL given will be checked for existence
(i.e., the URL actually loads and doesn't give a 404 response).</p>
<p>Note that when you're using the single-threaded development server,
validating a URL being served by the same server will hang. This should not
be a problem for multithreaded servers.</p>
</dd></dl>

<p>The admin represents this as an <tt class="docutils literal"><span class="pre">&lt;input</span> <span class="pre">type=&quot;text&quot;&gt;</span></tt> (a single-line input).</p>
<p>Like all <a class="reference internal" href="#django.db.models.CharField" title="django.db.models.CharField"><tt class="xref py py-class docutils literal"><span class="pre">CharField</span></tt></a> subclasses, <a class="reference internal" href="#django.db.models.URLField" title="django.db.models.URLField"><tt class="xref py py-class docutils literal"><span class="pre">URLField</span></tt></a> takes the optional
<tt class="xref py py-attr docutils literal"><span class="pre">max_length</span></tt>, a default of 200 is used.</p>
</div>
<div class="section" id="s-xmlfield">
<span id="xmlfield"></span><h3><tt class="docutils literal"><span class="pre">XMLField</span></tt><a class="headerlink" href="#xmlfield" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="django.db.models.XMLField">
<em class="property">class </em><tt class="descname">XMLField</tt>(<em>schema_path=None</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<a class="headerlink" href="#django.db.models.XMLField" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="#django.db.models.TextField" title="django.db.models.TextField"><tt class="xref py py-class docutils literal"><span class="pre">TextField</span></tt></a> that checks that the value is valid XML that matches a
given schema. Takes one required argument:</p>
<dl class="attribute">
<dt id="django.db.models.schema_path">
<tt class="descname">schema_path</tt><a class="headerlink" href="#django.db.models.schema_path" title="Permalink to this definition">¶</a></dt>
<dd><p>The filesystem path to a <a class="reference external" href="http://www.relaxng.org/">RelaxNG</a> schema against which to validate the
field.</p>
</dd></dl>

</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><tt class="docutils literal"><span class="pre">ForeignKey</span></tt><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><tt class="descname">ForeignKey</tt>(<em>othermodel</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<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 -- an object that has a many-to-one
relationship with itself -- use <tt class="docutils literal"><span class="pre">models.ForeignKey('self')</span></tt>.</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-python"><pre>class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Manufacturer(models.Model):
    # ...</pre>
</div>
<div class="versionadded">
<span class="title">New in Django 1.0:</span> <a class="reference internal" href="../../releases/1.0.html"><em>Please, see the release notes</em></a></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 <tt class="docutils literal"><span class="pre">Manufacturer</span></tt>
model above is defined in another application called <tt class="docutils literal"><span class="pre">production</span></tt>, you'd
need to use:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">&#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>
<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 <tt class="docutils literal"><span class="pre">&quot;_id&quot;</span></tt> to the field name to create its
database column name. In the above example, the database table for the <tt class="docutils literal"><span class="pre">Car</span></tt>
model will have a <tt class="docutils literal"><span class="pre">manufacturer_id</span></tt> 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"><tt class="xref py py-attr docutils literal"><span class="pre">db_column</span></tt></a>) However, your code should never have to
deal with the database column name, unless you write custom SQL. You'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"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> accepts an extra set of arguments -- all optional -- that
define the details of how the relation works.</p>
<dl class="attribute">
<dt id="django.db.models.ForeignKey.limit_choices_to">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">limit_choices_to</tt><a class="headerlink" href="#django.db.models.ForeignKey.limit_choices_to" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary of lookup arguments and values (see <a class="reference internal" href="../../topics/db/queries.html"><em>Making queries</em></a>)
that limit the available admin choices for this object. Use this with
functions from the Python <tt class="docutils literal"><span class="pre">datetime</span></tt> module to limit choices of objects by
date. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">limit_choices_to</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;pub_date__lte&#39;</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">}</span>
</pre></div>
</div>
<p>only allows the choice of related objects with a <tt class="docutils literal"><span class="pre">pub_date</span></tt> before the
current date/time to be chosen.</p>
<p>Instead of a dictionary this can also be a <tt class="xref py py-class docutils literal"><span class="pre">Q</span></tt>
object for more <a class="reference internal" href="../../topics/db/queries.html#complex-lookups-with-q"><em>complex queries</em></a>. However,
if <tt class="docutils literal"><span class="pre">limit_choices_to</span></tt> is a <tt class="xref py py-class docutils literal"><span class="pre">Q</span></tt> object then it
will only have an effect on the choices available in the admin when the
field is not listed in <tt class="docutils literal"><span class="pre">raw_id_fields</span></tt> in the <tt class="docutils literal"><span class="pre">ModelAdmin</span></tt> for the model.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.related_name">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">related_name</tt><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.
See the <a class="reference internal" href="../../topics/db/queries.html#backwards-related-objects"><em>related objects documentation</em></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"><em>abstract models</em></a>; and when you do so
<a class="reference internal" href="../../topics/db/models.html#abstract-related-name"><em>some special syntax</em></a> is available.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ForeignKey.to_field">
<tt class="descclassname">ForeignKey.</tt><tt class="descname">to_field</tt><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>

</div>
</div>
<div class="section" id="s-manytomanyfield">
<span id="s-ref-manytomany"></span><span id="manytomanyfield"></span><span id="ref-manytomany"></span><h3><tt class="docutils literal"><span class="pre">ManyToManyField</span></tt><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><tt class="descname">ManyToManyField</tt>(<em>othermodel</em><span class="optional">[</span>, <em>**options</em><span class="optional">]</span>)<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. This works exactly the same as it does for
<a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>, including all the options regarding <a class="reference internal" href="#recursive-relationships"><em>recursive</em></a> and <a class="reference internal" href="#lazy-relationships"><em>lazy</em></a> relationships.</p>
<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 model
that contains it. Since some databases don'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 <tt class="docutils literal"><span class="pre">author_books_9cdf4</span></tt>; 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"><tt class="xref py py-attr docutils literal"><span class="pre">db_table</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> accepts an extra set of arguments -- all optional --
that control how the relationship functions.</p>
<dl class="attribute">
<dt id="django.db.models.ManyToManyField.related_name">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">related_name</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">ForeignKey.related_name</span></tt></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.limit_choices_to">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">limit_choices_to</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">ForeignKey.limit_choices_to</span></tt></a>.</p>
<p><tt class="docutils literal"><span class="pre">limit_choices_to</span></tt> has no effect when used on a <tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> with a
custom intermediate table specified using the
<a class="reference internal" href="#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><tt class="xref py py-attr docutils literal"><span class="pre">through</span></tt></a> parameter.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.symmetrical">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">symmetrical</tt><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-python"><div class="highlight"><pre><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="s">&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"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> on itself, and as a result, it doesn't add a
<tt class="docutils literal"><span class="pre">person_set</span></tt> attribute to the <tt class="docutils literal"><span class="pre">Person</span></tt> class. Instead, the
<a class="reference internal" href="#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> is assumed to be symmetrical -- 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 <tt class="docutils literal"><span class="pre">self</span></tt>, set
<a class="reference internal" href="#django.db.models.ManyToManyField.symmetrical" title="django.db.models.ManyToManyField.symmetrical"><tt class="xref py py-attr docutils literal"><span class="pre">symmetrical</span></tt></a> to <tt class="xref docutils literal"><span class="pre">False</span></tt>. 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"><tt class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></tt></a> relationships to be non-symmetrical.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.through">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">through</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">through</span></tt></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"><em>extra data with a many-to-many relationship</em></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.db.models.ManyToManyField.db_table">
<tt class="descclassname">ManyToManyField.</tt><tt class="descname">db_table</tt><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 two tables being joined.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-onetoonefield">
<span id="s-ref-onetoone"></span><span id="onetoonefield"></span><span id="ref-onetoone"></span><h3><tt class="docutils literal"><span class="pre">OneToOneField</span></tt><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><tt class="descname">OneToOneField</tt>(<em>othermodel</em><span class="optional">[</span>, <em>parent_link=False</em>, <em>**options</em><span class="optional">]</span>)<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"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> with <a class="reference internal" href="#django.db.models.Field.unique" title="django.db.models.Field.unique"><tt class="xref py py-attr docutils literal"><span class="pre">unique=True</span></tt></a>, but the
&quot;reverse&quot; side of the relation will directly return a single object.</p>
<p>This is most useful as the primary key of a model which &quot;extends&quot;
another model in some way; <a class="reference internal" href="../../topics/db/models.html#multi-table-inheritance"><em>Multi-table inheritance</em></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"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>,
including all the options regarding <a class="reference internal" href="#recursive-relationships"><em>recursive</em></a>
and <a class="reference internal" href="#lazy-relationships"><em>lazy</em></a> relationships.</p>
<p id="onetoone-arguments">Additionally, <tt class="docutils literal"><span class="pre">OneToOneField</span></tt> accepts all of the extra arguments
accepted by <a class="reference internal" href="#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>, plus one extra argument:</p>
<dl class="attribute">
<dt id="django.db.models.OneToOneField.parent_link">
<tt class="descclassname">OneToOneField.</tt><tt class="descname">parent_link</tt><a class="headerlink" href="#django.db.models.OneToOneField.parent_link" title="Permalink to this definition">¶</a></dt>
<dd><p>When <tt class="xref docutils literal"><span class="pre">True</span></tt> and used in a model which inherits from another
(concrete) model, indicates that this field should be used as the
link back to the parent class, rather than the extra
<tt class="docutils literal"><span class="pre">OneToOneField</span></tt> which would normally be implicitly created by
subclassing.</p>
</dd></dl>

</div>
</div>
</div>


          </div>         
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <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"><tt class="docutils literal"><span class="pre">null</span></tt></a></li>
<li><a class="reference internal" href="#blank"><tt class="docutils literal"><span class="pre">blank</span></tt></a></li>
<li><a class="reference internal" href="#choices"><tt class="docutils literal"><span class="pre">choices</span></tt></a></li>
<li><a class="reference internal" href="#db-column"><tt class="docutils literal"><span class="pre">db_column</span></tt></a></li>
<li><a class="reference internal" href="#db-index"><tt class="docutils literal"><span class="pre">db_index</span></tt></a></li>
<li><a class="reference internal" href="#db-tablespace"><tt class="docutils literal"><span class="pre">db_tablespace</span></tt></a></li>
<li><a class="reference internal" href="#default"><tt class="docutils literal"><span class="pre">default</span></tt></a></li>
<li><a class="reference internal" href="#editable"><tt class="docutils literal"><span class="pre">editable</span></tt></a></li>
<li><a class="reference internal" href="#error-messages"><tt class="docutils literal"><span class="pre">error_messages</span></tt></a></li>
<li><a class="reference internal" href="#help-text"><tt class="docutils literal"><span class="pre">help_text</span></tt></a></li>
<li><a class="reference internal" href="#primary-key"><tt class="docutils literal"><span class="pre">primary_key</span></tt></a></li>
<li><a class="reference internal" href="#unique"><tt class="docutils literal"><span class="pre">unique</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-date"><tt class="docutils literal"><span class="pre">unique_for_date</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-month"><tt class="docutils literal"><span class="pre">unique_for_month</span></tt></a></li>
<li><a class="reference internal" href="#unique-for-year"><tt class="docutils literal"><span class="pre">unique_for_year</span></tt></a></li>
<li><a class="reference internal" href="#verbose-name"><tt class="docutils literal"><span class="pre">verbose_name</span></tt></a></li>
<li><a class="reference internal" href="#validators"><tt class="docutils literal"><span class="pre">validators</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#field-types">Field types</a><ul>
<li><a class="reference internal" href="#autofield"><tt class="docutils literal"><span class="pre">AutoField</span></tt></a></li>
<li><a class="reference internal" href="#bigintegerfield"><tt class="docutils literal"><span class="pre">BigIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#booleanfield"><tt class="docutils literal"><span class="pre">BooleanField</span></tt></a></li>
<li><a class="reference internal" href="#charfield"><tt class="docutils literal"><span class="pre">CharField</span></tt></a></li>
<li><a class="reference internal" href="#commaseparatedintegerfield"><tt class="docutils literal"><span class="pre">CommaSeparatedIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#datefield"><tt class="docutils literal"><span class="pre">DateField</span></tt></a></li>
<li><a class="reference internal" href="#datetimefield"><tt class="docutils literal"><span class="pre">DateTimeField</span></tt></a></li>
<li><a class="reference internal" href="#decimalfield"><tt class="docutils literal"><span class="pre">DecimalField</span></tt></a></li>
<li><a class="reference internal" href="#emailfield"><tt class="docutils literal"><span class="pre">EmailField</span></tt></a></li>
<li><a class="reference internal" href="#filefield"><tt class="docutils literal"><span class="pre">FileField</span></tt></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"><tt class="docutils literal"><span class="pre">FilePathField</span></tt></a></li>
<li><a class="reference internal" href="#floatfield"><tt class="docutils literal"><span class="pre">FloatField</span></tt></a></li>
<li><a class="reference internal" href="#imagefield"><tt class="docutils literal"><span class="pre">ImageField</span></tt></a></li>
<li><a class="reference internal" href="#integerfield"><tt class="docutils literal"><span class="pre">IntegerField</span></tt></a></li>
<li><a class="reference internal" href="#ipaddressfield"><tt class="docutils literal"><span class="pre">IPAddressField</span></tt></a></li>
<li><a class="reference internal" href="#nullbooleanfield"><tt class="docutils literal"><span class="pre">NullBooleanField</span></tt></a></li>
<li><a class="reference internal" href="#positiveintegerfield"><tt class="docutils literal"><span class="pre">PositiveIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#positivesmallintegerfield"><tt class="docutils literal"><span class="pre">PositiveSmallIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#slugfield"><tt class="docutils literal"><span class="pre">SlugField</span></tt></a></li>
<li><a class="reference internal" href="#smallintegerfield"><tt class="docutils literal"><span class="pre">SmallIntegerField</span></tt></a></li>
<li><a class="reference internal" href="#textfield"><tt class="docutils literal"><span class="pre">TextField</span></tt></a></li>
<li><a class="reference internal" href="#timefield"><tt class="docutils literal"><span class="pre">TimeField</span></tt></a></li>
<li><a class="reference internal" href="#urlfield"><tt class="docutils literal"><span class="pre">URLField</span></tt></a></li>
<li><a class="reference internal" href="#xmlfield"><tt class="docutils literal"><span class="pre">XMLField</span></tt></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"><tt class="docutils literal"><span class="pre">ForeignKey</span></tt></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"><tt class="docutils literal"><span class="pre">ManyToManyField</span></tt></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"><tt class="docutils literal"><span class="pre">OneToOneField</span></tt></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="index.html">Models</a></li>
    
    
      <li>Next: <a href="relations.html">Related objects reference</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django v1.2 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>  

  <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 id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Oct 20, 2010</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="relations.html" title="Related objects reference">next</a> &raquo;</div>
    </div>
  </div>

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