<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Model Meta options — Django 1.8.19 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.8.19', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="top" title="Django 1.8.19 documentation" href="../../contents.html" /> <link rel="up" title="Models" href="index.html" /> <link rel="next" title="Model instance reference" href="instances.html" /> <link rel="prev" title="Model class reference" href="class.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body role="document"> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.8.19 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="class.html" title="Model class reference">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="instances.html" title="Model instance reference">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-models-options"> <div class="section" id="s-model-meta-options"> <span id="model-meta-options"></span><h1>Model <code class="docutils literal"><span class="pre">Meta</span></code> options<a class="headerlink" href="#model-meta-options" title="Permalink to this headline">¶</a></h1> <p>This document explains all the possible <a class="reference internal" href="../../topics/db/models.html#meta-options"><span class="std std-ref">metadata options</span></a> that you can give your model in its internal <code class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></code>.</p> <div class="section" id="s-available-meta-options"> <span id="available-meta-options"></span><h2>Available <code class="docutils literal"><span class="pre">Meta</span></code> options<a class="headerlink" href="#available-meta-options" title="Permalink to this headline">¶</a></h2> <div class="section" id="s-abstract"> <span id="abstract"></span><h3><code class="docutils literal"><span class="pre">abstract</span></code><a class="headerlink" href="#abstract" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.abstract"> <code class="descclassname">Options.</code><code class="descname">abstract</code><a class="headerlink" href="#django.db.models.Options.abstract" title="Permalink to this definition">¶</a></dt> <dd><p>If <code class="docutils literal"><span class="pre">abstract</span> <span class="pre">=</span> <span class="pre">True</span></code>, this model will be an <a class="reference internal" href="../../topics/db/models.html#abstract-base-classes"><span class="std std-ref">abstract base class</span></a>.</p> </dd></dl> </div> <div class="section" id="s-app-label"> <span id="app-label"></span><h3><code class="docutils literal"><span class="pre">app_label</span></code><a class="headerlink" href="#app-label" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.app_label"> <code class="descclassname">Options.</code><code class="descname">app_label</code><a class="headerlink" href="#django.db.models.Options.app_label" title="Permalink to this definition">¶</a></dt> <dd><p>If a model exists outside of the standard locations (<code class="file docutils literal"><span class="pre">models.py</span></code> or a <code class="docutils literal"><span class="pre">models</span></code> package in an app), the model must define which app it is part of:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">app_label</span> <span class="o">=</span> <span class="s1">'myapp'</span> </pre></div> </div> <div class="versionadded"> <span class="title">New in Django 1.7:</span> <p><code class="docutils literal"><span class="pre">app_label</span></code> is no longer required for models that are defined outside the <code class="docutils literal"><span class="pre">models</span></code> module of an application.</p> </div> </dd></dl> </div> <div class="section" id="s-db-table"> <span id="db-table"></span><h3><code class="docutils literal"><span class="pre">db_table</span></code><a class="headerlink" href="#db-table" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.db_table"> <code class="descclassname">Options.</code><code class="descname">db_table</code><a class="headerlink" href="#django.db.models.Options.db_table" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the database table to use for the model:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">db_table</span> <span class="o">=</span> <span class="s1">'music_album'</span> </pre></div> </div> </dd></dl> <div class="section" id="s-table-names"> <span id="s-id1"></span><span id="table-names"></span><span id="id1"></span><h4>Table names<a class="headerlink" href="#table-names" title="Permalink to this headline">¶</a></h4> <p>To save you time, Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s “app label” – the name you used in <a class="reference internal" href="../django-admin.html#django-admin-startapp"><code class="xref std std-djadmin docutils literal"><span class="pre">manage.py</span> <span class="pre">startapp</span></code></a> – to the model’s class name, with an underscore between them.</p> <p>For example, if you have an app <code class="docutils literal"><span class="pre">bookstore</span></code> (as created by <code class="docutils literal"><span class="pre">manage.py</span> <span class="pre">startapp</span> <span class="pre">bookstore</span></code>), a model defined as <code class="docutils literal"><span class="pre">class</span> <span class="pre">Book</span></code> will have a database table named <code class="docutils literal"><span class="pre">bookstore_book</span></code>.</p> <p>To override the database table name, use the <code class="docutils literal"><span class="pre">db_table</span></code> parameter in <code class="docutils literal"><span class="pre">class</span> <span class="pre">Meta</span></code>.</p> <p>If your database table 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 class="admonition-use-lowercase-table-names-for-mysql admonition"> <p class="first admonition-title">Use lowercase table names for MySQL</p> <p class="last">It is strongly advised that you use lowercase table names when you override the table name via <code class="docutils literal"><span class="pre">db_table</span></code>, particularly if you are using the MySQL backend. See the <a class="reference internal" href="../databases.html#mysql-notes"><span class="std std-ref">MySQL notes</span></a> for more details.</p> </div> <div class="admonition-table-name-quoting-for-oracle admonition"> <p class="first admonition-title">Table name quoting for Oracle</p> <p>In order to meet the 30-char limitation Oracle has on table names, and match the usual conventions for Oracle databases, Django may shorten table names and turn them all-uppercase. To prevent such transformations, use a quoted name as the value for <code class="docutils literal"><span class="pre">db_table</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">db_table</span> <span class="o">=</span> <span class="s1">'"name_left_in_lowercase"'</span> </pre></div> </div> <p class="last">Such quoted names can also be used with Django’s other supported database backends; except for Oracle, however, the quotes have no effect. See the <a class="reference internal" href="../databases.html#oracle-notes"><span class="std std-ref">Oracle notes</span></a> for more details.</p> </div> </div> </div> <div class="section" id="s-db-tablespace"> <span id="db-tablespace"></span><h3><code class="docutils literal"><span class="pre">db_tablespace</span></code><a class="headerlink" href="#db-tablespace" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.db_tablespace"> <code class="descclassname">Options.</code><code class="descname">db_tablespace</code><a class="headerlink" href="#django.db.models.Options.db_tablespace" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the <a class="reference internal" href="../../topics/db/tablespaces.html"><span class="doc">database tablespace</span></a> to use for this model. The default is the project’s <a class="reference internal" href="../settings.html#std:setting-DEFAULT_TABLESPACE"><code class="xref std std-setting docutils literal"><span class="pre">DEFAULT_TABLESPACE</span></code></a> setting, if set. If the backend doesn’t support tablespaces, this option is ignored.</p> </dd></dl> </div> <div class="section" id="s-default-related-name"> <span id="default-related-name"></span><h3><code class="docutils literal"><span class="pre">default_related_name</span></code><a class="headerlink" href="#default-related-name" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.default_related_name"> <code class="descclassname">Options.</code><code class="descname">default_related_name</code><a class="headerlink" href="#django.db.models.Options.default_related_name" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.8.</span> </div> <p>The name that will be used by default for the relation from a related object back to this one. The default is <code class="docutils literal"><span class="pre"><model_name>_set</span></code>.</p> <p>As the reverse name for a field should be unique, be careful if you intend to subclass your model. To work around name collisions, part of the name should contain <code class="docutils literal"><span class="pre">'%(app_label)s'</span></code> and <code class="docutils literal"><span class="pre">'%(model_name)s'</span></code>, which are replaced respectively by the name of the application the model is in, and the name of the model, both lowercased. See the paragraph on <a class="reference internal" href="../../topics/db/models.html#abstract-related-name"><span class="std std-ref">related names for abstract models</span></a>.</p> </dd></dl> </div> <div class="section" id="s-get-latest-by"> <span id="get-latest-by"></span><h3><code class="docutils literal"><span class="pre">get_latest_by</span></code><a class="headerlink" href="#get-latest-by" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.get_latest_by"> <code class="descclassname">Options.</code><code class="descname">get_latest_by</code><a class="headerlink" href="#django.db.models.Options.get_latest_by" title="Permalink to this definition">¶</a></dt> <dd><p>The name of an orderable field in the model, typically a <a class="reference internal" href="fields.html#django.db.models.DateField" title="django.db.models.DateField"><code class="xref py py-class docutils literal"><span class="pre">DateField</span></code></a>, <a class="reference internal" href="fields.html#django.db.models.DateTimeField" title="django.db.models.DateTimeField"><code class="xref py py-class docutils literal"><span class="pre">DateTimeField</span></code></a>, or <a class="reference internal" href="fields.html#django.db.models.IntegerField" title="django.db.models.IntegerField"><code class="xref py py-class docutils literal"><span class="pre">IntegerField</span></code></a>. This specifies the default field to use in your model <a class="reference internal" href="../../topics/db/managers.html#django.db.models.Manager" title="django.db.models.Manager"><code class="xref py py-class docutils literal"><span class="pre">Manager</span></code></a>’s <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.latest" title="django.db.models.query.QuerySet.latest"><code class="xref py py-meth docutils literal"><span class="pre">latest()</span></code></a> and <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.earliest" title="django.db.models.query.QuerySet.earliest"><code class="xref py py-meth docutils literal"><span class="pre">earliest()</span></code></a> methods.</p> <p>Example:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">get_latest_by</span> <span class="o">=</span> <span class="s2">"order_date"</span> </pre></div> </div> <p>See the <a class="reference internal" href="querysets.html#django.db.models.query.QuerySet.latest" title="django.db.models.query.QuerySet.latest"><code class="xref py py-meth docutils literal"><span class="pre">latest()</span></code></a> docs for more.</p> </dd></dl> </div> <div class="section" id="s-managed"> <span id="managed"></span><h3><code class="docutils literal"><span class="pre">managed</span></code><a class="headerlink" href="#managed" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.managed"> <code class="descclassname">Options.</code><code class="descname">managed</code><a class="headerlink" href="#django.db.models.Options.managed" title="Permalink to this definition">¶</a></dt> <dd><p>Defaults to <code class="docutils literal"><span class="pre">True</span></code>, meaning Django will create the appropriate database tables in <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a> or as part of migrations and remove them as part of a <a class="reference internal" href="../django-admin.html#django-admin-flush"><code class="xref std std-djadmin docutils literal"><span class="pre">flush</span></code></a> management command. That is, Django <em>manages</em> the database tables’ lifecycles.</p> <p>If <code class="docutils literal"><span class="pre">False</span></code>, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the <em>only</em> difference when <code class="docutils literal"><span class="pre">managed=False</span></code>. All other aspects of model handling are exactly the same as normal. This includes</p> <ol class="arabic"> <li><p class="first">Adding an automatic primary key field to the model if you don’t declare it. To avoid confusion for later code readers, it’s recommended to specify all the columns from the database table you are modeling when using unmanaged models.</p> </li> <li><p class="first">If a model with <code class="docutils literal"><span class="pre">managed=False</span></code> contains a <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> that points to another unmanaged model, then the intermediate table for the many-to-many join will also not be created. However, the intermediary table between one managed and one unmanaged model <em>will</em> be created.</p> <p>If you need to change this default behavior, create the intermediary table as an explicit model (with <code class="docutils literal"><span class="pre">managed</span></code> set as needed) and use the <a class="reference internal" href="fields.html#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><code class="xref py py-attr docutils literal"><span class="pre">ManyToManyField.through</span></code></a> attribute to make the relation use your custom model.</p> </li> </ol> <p>For tests involving models with <code class="docutils literal"><span class="pre">managed=False</span></code>, it’s up to you to ensure the correct tables are created as part of the test setup.</p> <p>If you’re interested in changing the Python-level behavior of a model class, you <em>could</em> use <code class="docutils literal"><span class="pre">managed=False</span></code> and create a copy of an existing model. However, there’s a better approach for that situation: <a class="reference internal" href="../../topics/db/models.html#proxy-models"><span class="std std-ref">Proxy models</span></a>.</p> </dd></dl> </div> <div class="section" id="s-order-with-respect-to"> <span id="order-with-respect-to"></span><h3><code class="docutils literal"><span class="pre">order_with_respect_to</span></code><a class="headerlink" href="#order-with-respect-to" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.order_with_respect_to"> <code class="descclassname">Options.</code><code class="descname">order_with_respect_to</code><a class="headerlink" href="#django.db.models.Options.order_with_respect_to" title="Permalink to this definition">¶</a></dt> <dd><p>Makes this object orderable with respect to the given field, usually a <code class="docutils literal"><span class="pre">ForeignKey</span></code>. This can be used to make related objects orderable with respect to a parent object. For example, if an <code class="docutils literal"><span class="pre">Answer</span></code> relates to a <code class="docutils literal"><span class="pre">Question</span></code> object, and a question has more than one answer, and the order of answers matters, you’d do this:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span> <span class="k">class</span> <span class="nc">Question</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">text</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span> <span class="c1"># ...</span> <span class="k">class</span> <span class="nc">Answer</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">question</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Question</span><span class="p">)</span> <span class="c1"># ...</span> <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span> <span class="n">order_with_respect_to</span> <span class="o">=</span> <span class="s1">'question'</span> </pre></div> </div> <p>When <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> is set, two additional methods are provided to retrieve and to set the order of the related objects: <code class="docutils literal"><span class="pre">get_RELATED_order()</span></code> and <code class="docutils literal"><span class="pre">set_RELATED_order()</span></code>, where <code class="docutils literal"><span class="pre">RELATED</span></code> is the lowercased model name. For example, assuming that a <code class="docutils literal"><span class="pre">Question</span></code> object has multiple related <code class="docutils literal"><span class="pre">Answer</span></code> objects, the list returned contains the primary keys of the related <code class="docutils literal"><span class="pre">Answer</span></code> objects:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">question</span> <span class="o">=</span> <span class="n">Question</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">question</span><span class="o">.</span><span class="n">get_answer_order</span><span class="p">()</span> <span class="go">[1, 2, 3]</span> </pre></div> </div> <p>The order of a <code class="docutils literal"><span class="pre">Question</span></code> object’s related <code class="docutils literal"><span class="pre">Answer</span></code> objects can be set by passing in a list of <code class="docutils literal"><span class="pre">Answer</span></code> primary keys:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">question</span><span class="o">.</span><span class="n">set_answer_order</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span> </pre></div> </div> <p>The related objects also get two methods, <code class="docutils literal"><span class="pre">get_next_in_order()</span></code> and <code class="docutils literal"><span class="pre">get_previous_in_order()</span></code>, which can be used to access those objects in their proper order. Assuming the <code class="docutils literal"><span class="pre">Answer</span></code> objects are ordered by <code class="docutils literal"><span class="pre">id</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">answer</span> <span class="o">=</span> <span class="n">Answer</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">answer</span><span class="o">.</span><span class="n">get_next_in_order</span><span class="p">()</span> <span class="go"><Answer: 3></span> <span class="gp">>>> </span><span class="n">answer</span><span class="o">.</span><span class="n">get_previous_in_order</span><span class="p">()</span> <span class="go"><Answer: 1></span> </pre></div> </div> </dd></dl> <div class="admonition-order-with-respect-to-implicitly-sets-the-ordering-option admonition"> <p class="first admonition-title"><code class="docutils literal"><span class="pre">order_with_respect_to</span></code> implicitly sets the <code class="docutils literal"><span class="pre">ordering</span></code> option</p> <p class="last">Internally, <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> adds an additional field/database column named <code class="docutils literal"><span class="pre">_order</span></code> and sets the model’s <a class="reference internal" href="#django.db.models.Options.ordering" title="django.db.models.Options.ordering"><code class="xref py py-attr docutils literal"><span class="pre">ordering</span></code></a> option to this field. Consequently, <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> and <code class="docutils literal"><span class="pre">ordering</span></code> cannot be used together, and the ordering added by <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> will apply whenever you obtain a list of objects of this model.</p> </div> <div class="admonition-changing-order-with-respect-to admonition"> <p class="first admonition-title">Changing <code class="docutils literal"><span class="pre">order_with_respect_to</span></code></p> <p class="last">Because <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> adds a new database column, be sure to make and apply the appropriate migrations if you add or change <code class="docutils literal"><span class="pre">order_with_respect_to</span></code> after your initial <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a>.</p> </div> </div> <div class="section" id="s-ordering"> <span id="ordering"></span><h3><code class="docutils literal"><span class="pre">ordering</span></code><a class="headerlink" href="#ordering" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.ordering"> <code class="descclassname">Options.</code><code class="descname">ordering</code><a class="headerlink" href="#django.db.models.Options.ordering" title="Permalink to this definition">¶</a></dt> <dd><p>The default ordering for the object, for use when obtaining lists of objects:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'-order_date'</span><span class="p">]</span> </pre></div> </div> <p>This is a tuple or list of strings. Each string is a field name with an optional “-” prefix, which indicates descending order. Fields without a leading “-” will be ordered ascending. Use the string ”?” to order randomly.</p> <p>For example, to order by a <code class="docutils literal"><span class="pre">pub_date</span></code> field ascending, use this:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'pub_date'</span><span class="p">]</span> </pre></div> </div> <p>To order by <code class="docutils literal"><span class="pre">pub_date</span></code> descending, use this:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'-pub_date'</span><span class="p">]</span> </pre></div> </div> <p>To order by <code class="docutils literal"><span class="pre">pub_date</span></code> descending, then by <code class="docutils literal"><span class="pre">author</span></code> ascending, use this:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'-pub_date'</span><span class="p">,</span> <span class="s1">'author'</span><span class="p">]</span> </pre></div> </div> </dd></dl> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Ordering is not a free operation. Each field you add to the ordering incurs a cost to your database. Each foreign key you add will implicitly include all of its default orderings as well.</p> </div> </div> <div class="section" id="s-permissions"> <span id="permissions"></span><h3><code class="docutils literal"><span class="pre">permissions</span></code><a class="headerlink" href="#permissions" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.permissions"> <code class="descclassname">Options.</code><code class="descname">permissions</code><a class="headerlink" href="#django.db.models.Options.permissions" title="Permalink to this definition">¶</a></dt> <dd><p>Extra permissions to enter into the permissions table when creating this object. Add, delete and change permissions are automatically created for each model. This example specifies an extra permission, <code class="docutils literal"><span class="pre">can_deliver_pizzas</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">permissions</span> <span class="o">=</span> <span class="p">((</span><span class="s2">"can_deliver_pizzas"</span><span class="p">,</span> <span class="s2">"Can deliver pizzas"</span><span class="p">),)</span> </pre></div> </div> <p>This is a list or tuple of 2-tuples in the format <code class="docutils literal"><span class="pre">(permission_code,</span> <span class="pre">human_readable_permission_name)</span></code>.</p> </dd></dl> </div> <div class="section" id="s-default-permissions"> <span id="default-permissions"></span><h3><code class="docutils literal"><span class="pre">default_permissions</span></code><a class="headerlink" href="#default-permissions" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.default_permissions"> <code class="descclassname">Options.</code><code class="descname">default_permissions</code><a class="headerlink" href="#django.db.models.Options.default_permissions" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.7.</span> </div> <p>Defaults to <code class="docutils literal"><span class="pre">('add',</span> <span class="pre">'change',</span> <span class="pre">'delete')</span></code>. You may customize this list, for example, by setting this to an empty list if your app doesn’t require any of the default permissions. It must be specified on the model before the model is created by <a class="reference internal" href="../django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a> in order to prevent any omitted permissions from being created.</p> </dd></dl> </div> <div class="section" id="s-proxy"> <span id="proxy"></span><h3><code class="docutils literal"><span class="pre">proxy</span></code><a class="headerlink" href="#proxy" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.proxy"> <code class="descclassname">Options.</code><code class="descname">proxy</code><a class="headerlink" href="#django.db.models.Options.proxy" title="Permalink to this definition">¶</a></dt> <dd><p>If <code class="docutils literal"><span class="pre">proxy</span> <span class="pre">=</span> <span class="pre">True</span></code>, a model which subclasses another model will be treated as a <a class="reference internal" href="../../topics/db/models.html#proxy-models"><span class="std std-ref">proxy model</span></a>.</p> </dd></dl> </div> <div class="section" id="s-select-on-save"> <span id="select-on-save"></span><h3><code class="docutils literal"><span class="pre">select_on_save</span></code><a class="headerlink" href="#select-on-save" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.select_on_save"> <code class="descclassname">Options.</code><code class="descname">select_on_save</code><a class="headerlink" href="#django.db.models.Options.select_on_save" title="Permalink to this definition">¶</a></dt> <dd><p>Determines if Django will use the pre-1.6 <a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal"><span class="pre">django.db.models.Model.save()</span></code></a> algorithm. The old algorithm uses <code class="docutils literal"><span class="pre">SELECT</span></code> to determine if there is an existing row to be updated. The new algorithm tries an <code class="docutils literal"><span class="pre">UPDATE</span></code> directly. In some rare cases the <code class="docutils literal"><span class="pre">UPDATE</span></code> of an existing row isn’t visible to Django. An example is the PostgreSQL <code class="docutils literal"><span class="pre">ON</span> <span class="pre">UPDATE</span></code> trigger which returns <code class="docutils literal"><span class="pre">NULL</span></code>. In such cases the new algorithm will end up doing an <code class="docutils literal"><span class="pre">INSERT</span></code> even when a row exists in the database.</p> <p>Usually there is no need to set this attribute. The default is <code class="docutils literal"><span class="pre">False</span></code>.</p> <p>See <a class="reference internal" href="instances.html#django.db.models.Model.save" title="django.db.models.Model.save"><code class="xref py py-meth docutils literal"><span class="pre">django.db.models.Model.save()</span></code></a> for more about the old and new saving algorithm.</p> </dd></dl> </div> <div class="section" id="s-unique-together"> <span id="unique-together"></span><h3><code class="docutils literal"><span class="pre">unique_together</span></code><a class="headerlink" href="#unique-together" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.unique_together"> <code class="descclassname">Options.</code><code class="descname">unique_together</code><a class="headerlink" href="#django.db.models.Options.unique_together" title="Permalink to this definition">¶</a></dt> <dd><p>Sets of field names that, taken together, must be unique:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">unique_together</span> <span class="o">=</span> <span class="p">((</span><span class="s2">"driver"</span><span class="p">,</span> <span class="s2">"restaurant"</span><span class="p">),)</span> </pre></div> </div> <p>This is a tuple of tuples that must be unique when considered together. It’s used in the Django admin and is enforced at the database level (i.e., the appropriate <code class="docutils literal"><span class="pre">UNIQUE</span></code> statements are included in the <code class="docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span></code> statement).</p> <p>For convenience, unique_together can be a single tuple when dealing with a single set of fields:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">unique_together</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"driver"</span><span class="p">,</span> <span class="s2">"restaurant"</span><span class="p">)</span> </pre></div> </div> <p>A <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a> cannot be included in unique_together. (It’s not clear what that would even mean!) If you need to validate uniqueness related to a <a class="reference internal" href="fields.html#django.db.models.ManyToManyField" title="django.db.models.ManyToManyField"><code class="xref py py-class docutils literal"><span class="pre">ManyToManyField</span></code></a>, try using a signal or an explicit <a class="reference internal" href="fields.html#django.db.models.ManyToManyField.through" title="django.db.models.ManyToManyField.through"><code class="xref py py-attr docutils literal"><span class="pre">through</span></code></a> model.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.7:</span> <p>The <code class="docutils literal"><span class="pre">ValidationError</span></code> raised during model validation when the constraint is violated has the <code class="docutils literal"><span class="pre">unique_together</span></code> error code.</p> </div> </dd></dl> </div> <div class="section" id="s-index-together"> <span id="index-together"></span><h3><code class="docutils literal"><span class="pre">index_together</span></code><a class="headerlink" href="#index-together" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.index_together"> <code class="descclassname">Options.</code><code class="descname">index_together</code><a class="headerlink" href="#django.db.models.Options.index_together" title="Permalink to this definition">¶</a></dt> <dd><p>Sets of field names that, taken together, are indexed:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">index_together</span> <span class="o">=</span> <span class="p">[</span> <span class="p">[</span><span class="s2">"pub_date"</span><span class="p">,</span> <span class="s2">"deadline"</span><span class="p">],</span> <span class="p">]</span> </pre></div> </div> <p>This list of fields will be indexed together (i.e. the appropriate <code class="docutils literal"><span class="pre">CREATE</span> <span class="pre">INDEX</span></code> statement will be issued.)</p> <div class="versionchanged"> <span class="title">Changed in Django 1.7.</span> </div> <p>For convenience, <code class="docutils literal"><span class="pre">index_together</span></code> can be a single list when dealing with a single set of fields:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">index_together</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"pub_date"</span><span class="p">,</span> <span class="s2">"deadline"</span><span class="p">]</span> </pre></div> </div> </dd></dl> </div> <div class="section" id="s-verbose-name"> <span id="verbose-name"></span><h3><code class="docutils literal"><span class="pre">verbose_name</span></code><a class="headerlink" href="#verbose-name" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.verbose_name"> <code class="descclassname">Options.</code><code class="descname">verbose_name</code><a class="headerlink" href="#django.db.models.Options.verbose_name" title="Permalink to this definition">¶</a></dt> <dd><p>A human-readable name for the object, singular:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">verbose_name</span> <span class="o">=</span> <span class="s2">"pizza"</span> </pre></div> </div> <p>If this isn’t given, Django will use a munged version of the class name: <code class="docutils literal"><span class="pre">CamelCase</span></code> becomes <code class="docutils literal"><span class="pre">camel</span> <span class="pre">case</span></code>.</p> </dd></dl> </div> <div class="section" id="s-verbose-name-plural"> <span id="verbose-name-plural"></span><h3><code class="docutils literal"><span class="pre">verbose_name_plural</span></code><a class="headerlink" href="#verbose-name-plural" title="Permalink to this headline">¶</a></h3> <dl class="attribute"> <dt id="django.db.models.Options.verbose_name_plural"> <code class="descclassname">Options.</code><code class="descname">verbose_name_plural</code><a class="headerlink" href="#django.db.models.Options.verbose_name_plural" title="Permalink to this definition">¶</a></dt> <dd><p>The plural name for the object:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s2">"stories"</span> </pre></div> </div> <p>If this isn’t given, Django will use <a class="reference internal" href="#django.db.models.Options.verbose_name" title="django.db.models.Options.verbose_name"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name</span></code></a> + <code class="docutils literal"><span class="pre">"s"</span></code>.</p> </dd></dl> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Model <code class="docutils literal"><span class="pre">Meta</span></code> options</a><ul> <li><a class="reference internal" href="#available-meta-options">Available <code class="docutils literal"><span class="pre">Meta</span></code> options</a><ul> <li><a class="reference internal" href="#abstract"><code class="docutils literal"><span class="pre">abstract</span></code></a></li> <li><a class="reference internal" href="#app-label"><code class="docutils literal"><span class="pre">app_label</span></code></a></li> <li><a class="reference internal" href="#db-table"><code class="docutils literal"><span class="pre">db_table</span></code></a><ul> <li><a class="reference internal" href="#table-names">Table names</a></li> </ul> </li> <li><a class="reference internal" href="#db-tablespace"><code class="docutils literal"><span class="pre">db_tablespace</span></code></a></li> <li><a class="reference internal" href="#default-related-name"><code class="docutils literal"><span class="pre">default_related_name</span></code></a></li> <li><a class="reference internal" href="#get-latest-by"><code class="docutils literal"><span class="pre">get_latest_by</span></code></a></li> <li><a class="reference internal" href="#managed"><code class="docutils literal"><span class="pre">managed</span></code></a></li> <li><a class="reference internal" href="#order-with-respect-to"><code class="docutils literal"><span class="pre">order_with_respect_to</span></code></a></li> <li><a class="reference internal" href="#ordering"><code class="docutils literal"><span class="pre">ordering</span></code></a></li> <li><a class="reference internal" href="#permissions"><code class="docutils literal"><span class="pre">permissions</span></code></a></li> <li><a class="reference internal" href="#default-permissions"><code class="docutils literal"><span class="pre">default_permissions</span></code></a></li> <li><a class="reference internal" href="#proxy"><code class="docutils literal"><span class="pre">proxy</span></code></a></li> <li><a class="reference internal" href="#select-on-save"><code class="docutils literal"><span class="pre">select_on_save</span></code></a></li> <li><a class="reference internal" href="#unique-together"><code class="docutils literal"><span class="pre">unique_together</span></code></a></li> <li><a class="reference internal" href="#index-together"><code class="docutils literal"><span class="pre">index_together</span></code></a></li> <li><a class="reference internal" href="#verbose-name"><code class="docutils literal"><span class="pre">verbose_name</span></code></a></li> <li><a class="reference internal" href="#verbose-name-plural"><code class="docutils literal"><span class="pre">verbose_name_plural</span></code></a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="class.html">Model class reference</a></li> <li>Next: <a href="instances.html">Model instance reference</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.8.19 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">Models</a> <ul><li>Model <code class="docutils literal"><span class="pre">Meta</span></code> options</li></ul> </li></ul></li></ul> </li> </ul> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/models/options.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 10, 2018</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="class.html" title="Model class reference">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="instances.html" title="Model instance reference">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>