Sophie

Sophie

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

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

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


<html xmlns="http://www.w3.org/1999/xhtml" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Migrations &#8212; Django 1.8.19 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.8.19',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../contents.html" />
    <link rel="up" title="Using Django" href="index.html" />
    <link rel="next" title="Managing files" href="files.html" />
    <link rel="prev" title="Using mixins with class-based views" href="class-based-views/mixins.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 = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


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

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="class-based-views/mixins.html" title="Using mixins with class-based views">previous</a>
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="files.html" title="Managing files">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="topics-migrations">
            
  <div class="section" id="s-module-django.db.migrations">
<span id="s-migrations"></span><span id="module-django.db.migrations"></span><span id="migrations"></span><h1>Migrations<a class="headerlink" href="#module-django.db.migrations" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>Migrations are Django&#8217;s way of propagating changes you make to your models
(adding a field, deleting a model, etc.) into your database schema. They&#8217;re
designed to be mostly automatic, but you&#8217;ll need to know when to make
migrations, when to run them, and the common problems you might run into.</p>
<div class="section" id="s-a-brief-history">
<span id="a-brief-history"></span><h2>A Brief History<a class="headerlink" href="#a-brief-history" title="Permalink to this headline">¶</a></h2>
<p>Prior to version 1.7, Django only supported adding new models to the
database; it was not possible to alter or remove existing models via the
<code class="docutils literal"><span class="pre">syncdb</span></code> command (the predecessor to <a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a>).</p>
<p>Third-party tools, most notably <a class="reference external" href="http://south.aeracode.org">South</a>,
provided support for these additional types of change, but it was considered
important enough that support was brought into core Django.</p>
</div>
<div class="section" id="s-the-commands">
<span id="the-commands"></span><h2>The Commands<a class="headerlink" href="#the-commands" title="Permalink to this headline">¶</a></h2>
<p>There are several commands which you will use to interact with migrations
and Django&#8217;s handling of database schema:</p>
<ul class="simple">
<li><a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a>, which is responsible for applying migrations, as well as
unapplying and listing their status.</li>
<li><a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a>, which is responsible for creating new migrations
based on the changes you have made to your models.</li>
<li><a class="reference internal" href="../ref/django-admin.html#django-admin-sqlmigrate"><code class="xref std std-djadmin docutils literal"><span class="pre">sqlmigrate</span></code></a>, which displays the SQL statements for a migration.</li>
</ul>
<p>It&#8217;s worth noting that migrations are created and run on a per-app basis.
In particular, it&#8217;s possible to have apps that <em>do not use migrations</em> (these
are referred to as &#8220;unmigrated&#8221; apps) - these apps will instead mimic the
legacy behavior of just adding new models.</p>
<p>You should think of migrations as a version control system for your database
schema. <code class="docutils literal"><span class="pre">makemigrations</span></code> is responsible for packaging up your model changes
into individual migration files - analogous to commits - and <code class="docutils literal"><span class="pre">migrate</span></code> is
responsible for applying those to your database.</p>
<p>The migration files for each app live in a &#8220;migrations&#8221; directory inside
of that app, and are designed to be committed to, and distributed as part
of, its codebase. You should be making them once on your development machine
and then running the same migrations on your colleagues&#8217; machines, your
staging machines, and eventually your production machines.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It is possible to override the name of the package which contains the
migrations on a per-app basis by modifying the <a class="reference internal" href="../ref/settings.html#std:setting-MIGRATION_MODULES"><code class="xref std std-setting docutils literal"><span class="pre">MIGRATION_MODULES</span></code></a>
setting.</p>
</div>
<p>Migrations will run the same way on the same dataset and produce consistent
results, meaning that what you see in development and staging is, under the
same circumstances, exactly what will happen in production.</p>
<p>Django will make migrations for any change to your models or fields - even
options that don&#8217;t affect the database - as the only way it can reconstruct
a field correctly is to have all the changes in the history, and you might
need those options in some data migrations later on (for example, if you&#8217;ve
set custom validators).</p>
</div>
<div class="section" id="s-backend-support">
<span id="backend-support"></span><h2>Backend Support<a class="headerlink" href="#backend-support" title="Permalink to this headline">¶</a></h2>
<p>Migrations are supported on all backends that Django ships with, as well
as any third-party backends if they have programmed in support for schema
alteration (done via the <a class="reference internal" href="../ref/schema-editor.html"><span class="doc">SchemaEditor</span></a> class).</p>
<p>However, some databases are more capable than others when it comes to
schema migrations; some of the caveats are covered below.</p>
<div class="section" id="s-postgresql">
<span id="postgresql"></span><h3>PostgreSQL<a class="headerlink" href="#postgresql" title="Permalink to this headline">¶</a></h3>
<p>PostgreSQL is the most capable of all the databases here in terms of schema
support; the only caveat is that adding columns with default values will
cause a full rewrite of the table, for a time proportional to its size.</p>
<p>For this reason, it&#8217;s recommended you always create new columns with
<code class="docutils literal"><span class="pre">null=True</span></code>, as this way they will be added immediately.</p>
</div>
<div class="section" id="s-mysql">
<span id="mysql"></span><h3>MySQL<a class="headerlink" href="#mysql" title="Permalink to this headline">¶</a></h3>
<p>MySQL lacks support for transactions around schema alteration operations,
meaning that if a migration fails to apply you will have to manually unpick
the changes in order to try again (it&#8217;s impossible to roll back to an
earlier point).</p>
<p>In addition, MySQL will fully rewrite tables for almost every schema operation
and generally takes a time proportional to the number of rows in the table to
add or remove columns. On slower hardware this can be worse than a minute per
million rows - adding a few columns to a table with just a few million rows
could lock your site up for over ten minutes.</p>
<p>Finally, MySQL has reasonably small limits on name lengths for columns, tables
and indexes, as well as a limit on the combined size of all columns an index
covers. This means that indexes that are possible on other backends will
fail to be created under MySQL.</p>
</div>
<div class="section" id="s-sqlite">
<span id="sqlite"></span><h3>SQLite<a class="headerlink" href="#sqlite" title="Permalink to this headline">¶</a></h3>
<p>SQLite has very little built-in schema alteration support, and so Django
attempts to emulate it by:</p>
<ul class="simple">
<li>Creating a new table with the new schema</li>
<li>Copying the data across</li>
<li>Dropping the old table</li>
<li>Renaming the new table to match the original name</li>
</ul>
<p>This process generally works well, but it can be slow and occasionally
buggy. It is not recommended that you run and migrate SQLite in a
production environment unless you are very aware of the risks and
its limitations; the support Django ships with is designed to allow
developers to use SQLite on their local machines to develop less complex
Django projects without the need for a full database.</p>
</div>
</div>
<div class="section" id="s-workflow">
<span id="workflow"></span><h2>Workflow<a class="headerlink" href="#workflow" title="Permalink to this headline">¶</a></h2>
<p>Working with migrations is simple. Make changes to your models - say, add
a field and remove a model - and then run <a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python manage.py makemigrations
Migrations for &#39;books&#39;:
  0003_auto.py:
    - Alter field author on book
</pre></div>
</div>
<p>Your models will be scanned and compared to the versions currently
contained in your migration files, and then a new set of migrations
will be written out. Make sure to read the output to see what
<code class="docutils literal"><span class="pre">makemigrations</span></code> thinks you have changed - it&#8217;s not perfect, and for
complex changes it might not be detecting what you expect.</p>
<p>Once you have your new migration files, you should apply them to your
database to make sure they work as expected:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: sessions, admin, messages, auth, staticfiles, contenttypes
  Apply all migrations: books
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  Applying books.0003_auto... OK
</pre></div>
</div>
<p>The command runs in two stages; first, it synchronizes unmigrated apps
(performing the same functionality that <code class="docutils literal"><span class="pre">syncdb</span></code> used to provide), and
then it runs any migrations that have not yet been applied.</p>
<p>Once the migration is applied, commit the migration and the models change
to your version control system as a single commit - that way, when other
developers (or your production servers) check out the code, they&#8217;ll
get both the changes to your models and the accompanying migration at the
same time.</p>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>If you want to give the migration(s) a meaningful name instead of a generated
one, you can use the <a class="reference internal" href="../ref/django-admin.html#django-admin-option---name"><code class="xref std std-djadminopt docutils literal"><span class="pre">--name</span></code></a> option:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python manage.py makemigrations --name changed_my_model your_app_label
</pre></div>
</div>
<div class="section" id="s-version-control">
<span id="version-control"></span><h3>Version control<a class="headerlink" href="#version-control" title="Permalink to this headline">¶</a></h3>
<p>Because migrations are stored in version control, you&#8217;ll occasionally
come across situations where you and another developer have both committed
a migration to the same app at the same time, resulting in two migrations
with the same number.</p>
<p>Don&#8217;t worry - the numbers are just there for developers&#8217; reference, Django
just cares that each migration has a different name. Migrations specify which
other migrations they depend on - including earlier migrations in the same
app - in the file, so it&#8217;s possible to detect when there&#8217;s two new migrations
for the same app that aren&#8217;t ordered.</p>
<p>When this happens, Django will prompt you and give you some options. If it
thinks it&#8217;s safe enough, it will offer to automatically linearize the two
migrations for you. If not, you&#8217;ll have to go in and modify the migrations
yourself - don&#8217;t worry, this isn&#8217;t difficult, and is explained more in
<a class="reference internal" href="#migration-files"><span class="std std-ref">Migration files</span></a> below.</p>
</div>
</div>
<div class="section" id="s-dependencies">
<span id="dependencies"></span><h2>Dependencies<a class="headerlink" href="#dependencies" title="Permalink to this headline">¶</a></h2>
<p>While migrations are per-app, the tables and relationships implied by
your models are too complex to be created for just one app at a time. When
you make a migration that requires something else to run - for example,
you add a <code class="docutils literal"><span class="pre">ForeignKey</span></code> in your <code class="docutils literal"><span class="pre">books</span></code> app to your <code class="docutils literal"><span class="pre">authors</span></code> app - the
resulting migration will contain a dependency on a migration in <code class="docutils literal"><span class="pre">authors</span></code>.</p>
<p>This means that when you run the migrations, the <code class="docutils literal"><span class="pre">authors</span></code> migration runs
first and creates the table the <code class="docutils literal"><span class="pre">ForeignKey</span></code> references, and then the migration
that makes the <code class="docutils literal"><span class="pre">ForeignKey</span></code> column runs afterwards and creates the constraint.
If this didn&#8217;t happen, the migration would try to create the <code class="docutils literal"><span class="pre">ForeignKey</span></code>
column without the table it&#8217;s referencing existing and your database would
throw an error.</p>
<p>This dependency behavior affects most migration operations where you
restrict to a single app. Restricting to a single app (either in
<code class="docutils literal"><span class="pre">makemigrations</span></code> or <code class="docutils literal"><span class="pre">migrate</span></code>) is a best-efforts promise, and not
a guarantee; any other apps that need to be used to get dependencies correct
will be.</p>
<p id="unmigrated-dependencies">Be aware, however, that unmigrated apps cannot depend on migrated apps, by the
very nature of not having migrations. This means that it is not generally
possible to have an unmigrated app have a <code class="docutils literal"><span class="pre">ForeignKey</span></code> or <code class="docutils literal"><span class="pre">ManyToManyField</span></code>
to a migrated app; some cases may work, but it will eventually fail.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Even if things appear to work with unmigrated apps depending on migrated
apps, Django may not generate all the necessary foreign key constraints!</p>
</div>
<p>This is particularly apparent if you use swappable models (e.g.
<code class="docutils literal"><span class="pre">AUTH_USER_MODEL</span></code>), as every app that uses swappable models will need
to have migrations if you&#8217;re unlucky. As time goes on, more and more
third-party apps will get migrations, but in the meantime you can either
give them migrations yourself (using <a class="reference internal" href="../ref/settings.html#std:setting-MIGRATION_MODULES"><code class="xref std std-setting docutils literal"><span class="pre">MIGRATION_MODULES</span></code></a> to
store those modules outside of the app&#8217;s own module if you wish), or
keep the app with your user model unmigrated.</p>
<p>In addition, any models that are used in <code class="docutils literal"><span class="pre">RunPython</span></code> operations must have
migrations so that their relations to other models are properly created.</p>
</div>
<div class="section" id="s-migration-files">
<span id="s-id1"></span><span id="migration-files"></span><span id="id1"></span><h2>Migration files<a class="headerlink" href="#migration-files" title="Permalink to this headline">¶</a></h2>
<p>Migrations are stored as an on-disk format, referred to here as
&#8220;migration files&#8221;. These files are actually just normal Python files with
an agreed-upon object layout, written in a declarative style.</p>
<p>A basic migration file looks like 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">migrations</span><span class="p">,</span> <span class="n">models</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>

    <span class="n">dependencies</span> <span class="o">=</span> <span class="p">[(</span><span class="s2">&quot;migrations&quot;</span><span class="p">,</span> <span class="s2">&quot;0001_initial&quot;</span><span class="p">)]</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">migrations</span><span class="o">.</span><span class="n">DeleteModel</span><span class="p">(</span><span class="s2">&quot;Tribble&quot;</span><span class="p">),</span>
        <span class="n">migrations</span><span class="o">.</span><span class="n">AddField</span><span class="p">(</span><span class="s2">&quot;Author&quot;</span><span class="p">,</span> <span class="s2">&quot;rating&quot;</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">)),</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>What Django looks for when it loads a migration file (as a Python module) is
a subclass of <code class="docutils literal"><span class="pre">django.db.migrations.Migration</span></code> called <code class="docutils literal"><span class="pre">Migration</span></code>. It then
inspects this object for four attributes, only two of which are used
most of the time:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">dependencies</span></code>, a list of migrations this one depends on.</li>
<li><code class="docutils literal"><span class="pre">operations</span></code>, a list of <code class="docutils literal"><span class="pre">Operation</span></code> classes that define what this
migration does.</li>
</ul>
<p>The operations are the key; they are a set of declarative instructions which
tell Django what schema changes need to be made. Django scans them and
builds an in-memory representation of all of the schema changes to all apps,
and uses this to generate the SQL which makes the schema changes.</p>
<p>That in-memory structure is also used to work out what the differences are
between your models and the current state of your migrations; Django runs
through all the changes, in order, on an in-memory set of models to come
up with the state of your models last time you ran <code class="docutils literal"><span class="pre">makemigrations</span></code>. It
then uses these models to compare against the ones in your <code class="docutils literal"><span class="pre">models.py</span></code> files
to work out what you have changed.</p>
<p>You should rarely, if ever, need to edit migration files by hand, but
it&#8217;s entirely possible to write them manually if you need to. Some of the
more complex operations are not autodetectable and are only available via
a hand-written migration, so don&#8217;t be scared about editing them if you have to.</p>
<div class="section" id="s-custom-fields">
<span id="custom-fields"></span><h3>Custom fields<a class="headerlink" href="#custom-fields" title="Permalink to this headline">¶</a></h3>
<p>You can&#8217;t modify the number of positional arguments in an already migrated
custom field without raising a <code class="docutils literal"><span class="pre">TypeError</span></code>. The old migration will call the
modified <code class="docutils literal"><span class="pre">__init__</span></code> method with the old signature. So if you need a new
argument, please create a keyword argument and add something like
<code class="docutils literal"><span class="pre">assert</span> <span class="pre">'argument_name'</span> <span class="pre">in</span> <span class="pre">kwargs</span></code> in the constructor.</p>
</div>
<div class="section" id="s-model-managers">
<span id="s-using-managers-in-migrations"></span><span id="model-managers"></span><span id="using-managers-in-migrations"></span><h3>Model managers<a class="headerlink" href="#model-managers" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>You can optionally serialize managers into migrations and have them available
in <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> operations. This is done
by defining a <code class="docutils literal"><span class="pre">use_in_migrations</span></code> attribute on the manager class:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyManager</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Manager</span><span class="p">):</span>
    <span class="n">use_in_migrations</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">MyManager</span><span class="p">()</span>
</pre></div>
</div>
<p>If you are using the <a class="reference internal" href="db/managers.html#django.db.models.from_queryset" title="django.db.models.from_queryset"><code class="xref py py-meth docutils literal"><span class="pre">from_queryset()</span></code></a> function to
dynamically generate a manager class, you need to inherit from the generated
class to make it importable:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyManager</span><span class="p">(</span><span class="n">MyBaseManager</span><span class="o">.</span><span class="n">from_queryset</span><span class="p">(</span><span class="n">CustomQuerySet</span><span class="p">)):</span>
    <span class="n">use_in_migrations</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">MyManager</span><span class="p">()</span>
</pre></div>
</div>
<p>Please refer to the notes about <a class="reference internal" href="#historical-models"><span class="std std-ref">Historical models</span></a> in migrations to see
the implications that come along.</p>
</div>
</div>
<div class="section" id="s-adding-migrations-to-apps">
<span id="adding-migrations-to-apps"></span><h2>Adding migrations to apps<a class="headerlink" href="#adding-migrations-to-apps" title="Permalink to this headline">¶</a></h2>
<p>Adding migrations to new apps is straightforward - they come preconfigured to
accept migrations, and so just run <a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a> once you&#8217;ve made
some changes.</p>
<p>If your app already has models and database tables, and doesn&#8217;t have migrations
yet (for example, you created it against a previous Django version), you&#8217;ll
need to convert it to use migrations; this is a simple process:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ python manage.py makemigrations your_app_label
</pre></div>
</div>
<p>This will make a new initial migration for your app. Now, run <code class="docutils literal"><span class="pre">python</span>
<span class="pre">manage.py</span> <span class="pre">migrate</span> <span class="pre">--fake-initial</span></code>, and Django will detect that you have an
initial migration <em>and</em> that the tables it wants to create already exist, and
will mark the migration as already applied. (Without the
<a class="reference internal" href="../ref/django-admin.html#django-admin-option---fake-initial"><code class="xref std std-djadminopt docutils literal"><span class="pre">--fake-initial</span></code></a> flag, the <a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a> command would error
out because the tables it wants to create already exist.)</p>
<p>Note that this only works given two things:</p>
<ul class="simple">
<li>You have not changed your models since you made their tables. For migrations
to work, you must make the initial migration <em>first</em> and then make changes,
as Django compares changes against migration files, not the database.</li>
<li>You have not manually edited your database - Django won&#8217;t be able to detect
that your database doesn&#8217;t match your models, you&#8217;ll just get errors when
migrations try to modify those tables.</li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">--fake-initial</span></code> flag to <a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a> was added. Previously,
Django would always automatically fake-apply initial migrations if it
detected that the tables exist.</p>
</div>
</div>
<div class="section" id="s-historical-models">
<span id="s-id2"></span><span id="historical-models"></span><span id="id2"></span><h2>Historical models<a class="headerlink" href="#historical-models" title="Permalink to this headline">¶</a></h2>
<p>When you run migrations, Django is working from historical versions of your
models stored in the migration files. If you write Python code using the
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> operation, or if you have
<code class="docutils literal"><span class="pre">allow_migrate</span></code> methods on your database routers, you will be exposed to
these versions of your models.</p>
<p>Because it&#8217;s impossible to serialize arbitrary Python code, these historical
models will not have any custom methods that you have defined. They will,
however, have the same fields, relationships, managers (limited to those with
<code class="docutils literal"><span class="pre">use_in_migrations</span> <span class="pre">=</span> <span class="pre">True</span></code>) and <code class="docutils literal"><span class="pre">Meta</span></code> options (also versioned, so they may
be different from your current ones).</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">This means that you will NOT have custom <code class="docutils literal"><span class="pre">save()</span></code> methods called on objects
when you access them in migrations, and you will NOT have any custom
constructors or instance methods. Plan appropriately!</p>
</div>
<p>References to functions in field options such as <code class="docutils literal"><span class="pre">upload_to</span></code> and
<code class="docutils literal"><span class="pre">limit_choices_to</span></code> and model manager declarations with managers having
<code class="docutils literal"><span class="pre">use_in_migrations</span> <span class="pre">=</span> <span class="pre">True</span></code> are serialized in migrations, so the functions and
classes will need to be kept around for as long as there is a migration
referencing them. Any <a class="reference internal" href="../howto/custom-model-fields.html"><span class="doc">custom model fields</span></a>
will also need to be kept, since these are imported directly by migrations.</p>
<p>In addition, the base classes of the model are just stored as pointers, so you
must always keep base classes around for as long as there is a migration that
contains a reference to them. On the plus side, methods and managers from these
base classes inherit normally, so if you absolutely need access to these you
can opt to move them into a superclass.</p>
</div>
<div class="section" id="s-considerations-when-removing-model-fields">
<span id="s-migrations-removing-model-fields"></span><span id="considerations-when-removing-model-fields"></span><span id="migrations-removing-model-fields"></span><h2>Considerations when removing model fields<a class="headerlink" href="#considerations-when-removing-model-fields" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 1.8.</span> </div>
<p>Similar to the &#8220;references to historical functions&#8221; considerations described in
the previous section, removing custom model fields from your project or
third-party app will cause a problem if they are referenced in old migrations.</p>
<p>To help with this situation, Django provides some model field attributes to
assist with model field deprecation using the <a class="reference internal" href="checks.html"><span class="doc">system checks framework</span></a>.</p>
<p>Add the <code class="docutils literal"><span class="pre">system_check_deprecated_details</span></code> attribute to your model field
similar to the following:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">IPAddressField</span><span class="p">(</span><span class="n">Field</span><span class="p">):</span>
    <span class="n">system_check_deprecated_details</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="p">(</span>
            <span class="s1">&#39;IPAddressField has been deprecated. Support for it (except &#39;</span>
            <span class="s1">&#39;in historical migrations) will be removed in Django 1.9.&#39;</span>
        <span class="p">),</span>
        <span class="s1">&#39;hint&#39;</span><span class="p">:</span> <span class="s1">&#39;Use GenericIPAddressField instead.&#39;</span><span class="p">,</span>  <span class="c1"># optional</span>
        <span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="s1">&#39;fields.W900&#39;</span><span class="p">,</span>  <span class="c1"># pick a unique ID for your field.</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>After a deprecation period of your choosing (two major releases for fields in
Django itself), change the <code class="docutils literal"><span class="pre">system_check_deprecated_details</span></code> attribute to
<code class="docutils literal"><span class="pre">system_check_removed_details</span></code> and update the dictionary similar to:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">IPAddressField</span><span class="p">(</span><span class="n">Field</span><span class="p">):</span>
    <span class="n">system_check_removed_details</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="p">(</span>
            <span class="s1">&#39;IPAddressField has been removed except for support in &#39;</span>
            <span class="s1">&#39;historical migrations.&#39;</span>
        <span class="p">),</span>
        <span class="s1">&#39;hint&#39;</span><span class="p">:</span> <span class="s1">&#39;Use GenericIPAddressField instead.&#39;</span><span class="p">,</span>
        <span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="s1">&#39;fields.E900&#39;</span><span class="p">,</span>  <span class="c1"># pick a unique ID for your field.</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>You should keep the field&#8217;s methods that are required for it to operate in
database migrations such as <code class="docutils literal"><span class="pre">__init__()</span></code>, <code class="docutils literal"><span class="pre">deconstruct()</span></code>, and
<code class="docutils literal"><span class="pre">get_internal_type()</span></code>. Keep this stub field for as long as any migrations
which reference the field exist. For example, after squashing migrations and
removing the old ones, you should be able to remove the field completely.</p>
</div>
<div class="section" id="s-data-migrations">
<span id="s-id3"></span><span id="data-migrations"></span><span id="id3"></span><h2>Data Migrations<a class="headerlink" href="#data-migrations" title="Permalink to this headline">¶</a></h2>
<p>As well as changing the database schema, you can also use migrations to change
the data in the database itself, in conjunction with the schema if you want.</p>
<p>Migrations that alter data are usually called &#8220;data migrations&#8221;; they&#8217;re best
written as separate migrations, sitting alongside your schema migrations.</p>
<p>Django can&#8217;t automatically generate data migrations for you, as it does with
schema migrations, but it&#8217;s not very hard to write them. Migration files in
Django are made up of <a class="reference internal" href="../ref/migration-operations.html"><span class="doc">Operations</span></a>, and
the main operation you use for data migrations is
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a>.</p>
<p>To start, make an empty migration file you can work from (Django will put
the file in the right place, suggest a name, and add dependencies for you):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">manage</span><span class="o">.</span><span class="n">py</span> <span class="n">makemigrations</span> <span class="o">--</span><span class="n">empty</span> <span class="n">yourappname</span>
</pre></div>
</div>
<p>Then, open up the file; it should look something like this:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span><span class="p">,</span> <span class="n">migrations</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>

    <span class="n">dependencies</span> <span class="o">=</span> <span class="p">[</span>
        <span class="p">(</span><span class="s1">&#39;yourappname&#39;</span><span class="p">,</span> <span class="s1">&#39;0001_initial&#39;</span><span class="p">),</span>
    <span class="p">]</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>Now, all you need to do is create a new function and have
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> use it.
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> expects a callable as its argument
which takes two arguments - the first is an <a class="reference internal" href="../ref/applications.html"><span class="doc">app registry</span></a> that has the historical versions of all your models
loaded into it to match where in your history the migration sits, and the
second is a <a class="reference internal" href="../ref/schema-editor.html"><span class="doc">SchemaEditor</span></a>, which you can use to
manually effect database schema changes (but beware, doing this can confuse
the migration autodetector!)</p>
<p>Let&#8217;s write a simple migration that populates our new <code class="docutils literal"><span class="pre">name</span></code> field with the
combined values of <code class="docutils literal"><span class="pre">first_name</span></code> and <code class="docutils literal"><span class="pre">last_name</span></code> (we&#8217;ve come to our senses
and realized that not everyone has first and last names). All we
need to do is use the historical model and iterate over the rows:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span><span class="p">,</span> <span class="n">migrations</span>

<span class="k">def</span> <span class="nf">combine_names</span><span class="p">(</span><span class="n">apps</span><span class="p">,</span> <span class="n">schema_editor</span><span class="p">):</span>
    <span class="c1"># We can&#39;t import the Person model directly as it may be a newer</span>
    <span class="c1"># version than this migration expects. We use the historical version.</span>
    <span class="n">Person</span> <span class="o">=</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_model</span><span class="p">(</span><span class="s2">&quot;yourappname&quot;</span><span class="p">,</span> <span class="s2">&quot;Person&quot;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">person</span> <span class="ow">in</span> <span class="n">Person</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
        <span class="n">person</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">person</span><span class="o">.</span><span class="n">first_name</span><span class="p">,</span> <span class="n">person</span><span class="o">.</span><span class="n">last_name</span><span class="p">)</span>
        <span class="n">person</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>

    <span class="n">dependencies</span> <span class="o">=</span> <span class="p">[</span>
        <span class="p">(</span><span class="s1">&#39;yourappname&#39;</span><span class="p">,</span> <span class="s1">&#39;0001_initial&#39;</span><span class="p">),</span>
    <span class="p">]</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">migrations</span><span class="o">.</span><span class="n">RunPython</span><span class="p">(</span><span class="n">combine_names</span><span class="p">),</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>Once that&#8217;s done, we can just run <code class="docutils literal"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">migrate</span></code> as normal and
the data migration will run in place alongside other migrations.</p>
<p>You can pass a second callable to
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> to run whatever logic you
want executed when migrating backwards. If this callable is omitted, migrating
backwards will raise an exception.</p>
<div class="section" id="s-accessing-models-from-other-apps">
<span id="accessing-models-from-other-apps"></span><h3>Accessing models from other apps<a class="headerlink" href="#accessing-models-from-other-apps" title="Permalink to this headline">¶</a></h3>
<p>When writing a <code class="docutils literal"><span class="pre">RunPython</span></code> function that uses models from apps other than the
one in which the migration is located, the migration&#8217;s <code class="docutils literal"><span class="pre">dependencies</span></code>
attribute should include the latest migration of each app that is involved,
otherwise you may get an error similar to: <code class="docutils literal"><span class="pre">LookupError:</span> <span class="pre">No</span> <span class="pre">installed</span> <span class="pre">app</span>
<span class="pre">with</span> <span class="pre">label</span> <span class="pre">'myappname'</span></code> when you try to retrieve the model in the <code class="docutils literal"><span class="pre">RunPython</span></code>
function using <code class="docutils literal"><span class="pre">apps.get_model()</span></code>.</p>
<p>In the following example, we have a migration in <code class="docutils literal"><span class="pre">app1</span></code> which needs to use
models in <code class="docutils literal"><span class="pre">app2</span></code>. We aren&#8217;t concerned with the details of <code class="docutils literal"><span class="pre">move_m1</span></code> other
than the fact it will need to access models from both apps. Therefore we&#8217;ve
added a dependency that specifies the last migration of <code class="docutils literal"><span class="pre">app2</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>

    <span class="n">dependencies</span> <span class="o">=</span> <span class="p">[</span>
        <span class="p">(</span><span class="s1">&#39;app1&#39;</span><span class="p">,</span> <span class="s1">&#39;0001_initial&#39;</span><span class="p">),</span>
        <span class="c1"># added dependency to enable using models from app2 in move_m1</span>
        <span class="p">(</span><span class="s1">&#39;app2&#39;</span><span class="p">,</span> <span class="s1">&#39;0004_foobar&#39;</span><span class="p">),</span>
    <span class="p">]</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">migrations</span><span class="o">.</span><span class="n">RunPython</span><span class="p">(</span><span class="n">move_m1</span><span class="p">),</span>
    <span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-more-advanced-migrations">
<span id="more-advanced-migrations"></span><h3>More advanced migrations<a class="headerlink" href="#more-advanced-migrations" title="Permalink to this headline">¶</a></h3>
<p>If you&#8217;re interested in the more advanced migration operations, or want
to be able to write your own, see the <a class="reference internal" href="../ref/migration-operations.html"><span class="doc">migration operations reference</span></a> and the &#8220;how-to&#8221; on <a class="reference internal" href="../howto/writing-migrations.html"><span class="doc">writing migrations</span></a>.</p>
</div>
</div>
<div class="section" id="s-squashing-migrations">
<span id="s-migration-squashing"></span><span id="squashing-migrations"></span><span id="migration-squashing"></span><h2>Squashing migrations<a class="headerlink" href="#squashing-migrations" title="Permalink to this headline">¶</a></h2>
<p>You are encouraged to make migrations freely and not worry about how many you
have; the migration code is optimized to deal with hundreds at a time without
much slowdown. However, eventually you will want to move back from having
several hundred migrations to just a few, and that&#8217;s where squashing comes in.</p>
<p>Squashing is the act of reducing an existing set of many migrations down to
one (or sometimes a few) migrations which still represent the same changes.</p>
<p>Django does this by taking all of your existing migrations, extracting their
<code class="docutils literal"><span class="pre">Operation</span></code>s and putting them all in sequence, and then running an optimizer
over them to try and reduce the length of the list - for example, it knows
that <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.CreateModel" title="django.db.migrations.operations.CreateModel"><code class="xref py py-class docutils literal"><span class="pre">CreateModel</span></code></a> and
<a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.DeleteModel" title="django.db.migrations.operations.DeleteModel"><code class="xref py py-class docutils literal"><span class="pre">DeleteModel</span></code></a> cancel each other out,
and it knows that <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.AddField" title="django.db.migrations.operations.AddField"><code class="xref py py-class docutils literal"><span class="pre">AddField</span></code></a> can be
rolled into <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.CreateModel" title="django.db.migrations.operations.CreateModel"><code class="xref py py-class docutils literal"><span class="pre">CreateModel</span></code></a>.</p>
<p>Once the operation sequence has been reduced as much as possible - the amount
possible depends on how closely intertwined your models are and if you have
any <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunSQL" title="django.db.migrations.operations.RunSQL"><code class="xref py py-class docutils literal"><span class="pre">RunSQL</span></code></a>
or <a class="reference internal" href="../ref/migration-operations.html#django.db.migrations.operations.RunPython" title="django.db.migrations.operations.RunPython"><code class="xref py py-class docutils literal"><span class="pre">RunPython</span></code></a> operations (which can&#8217;t
be optimized through) - Django will then write it back out into a new set of
initial migration files.</p>
<p>These files are marked to say they replace the previously-squashed migrations,
so they can coexist with the old migration files, and Django will intelligently
switch between them depending where you are in the history. If you&#8217;re still
part-way through the set of migrations that you squashed, it will keep using
them until it hits the end and then switch to the squashed history, while new
installs will just use the new squashed migration and skip all the old ones.</p>
<p>This enables you to squash and not mess up systems currently in production
that aren&#8217;t fully up-to-date yet. The recommended process is to squash, keeping
the old files, commit and release, wait until all systems are upgraded with
the new release (or if you&#8217;re a third-party project, just ensure your users
upgrade releases in order without skipping any), and then remove the old files,
commit and do a second release.</p>
<p>The command that backs all this is <a class="reference internal" href="../ref/django-admin.html#django-admin-squashmigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">squashmigrations</span></code></a> - just pass
it the app label and migration name you want to squash up to, and it&#8217;ll get to
work:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ ./manage.py squashmigrations myapp 0004
Will squash the following migrations:
 - 0001_initial
 - 0002_some_change
 - 0003_another_change
 - 0004_undo_something
Do you wish to proceed? [yN] y
Optimizing...
  Optimized from 12 operations to 7 operations.
Created new squashed migration /home/andrew/Programs/DjangoTest/test/migrations/0001_squashed_0004_undo_somthing.py
  You should commit this migration but leave the old ones in place;
  the new migration will be used for new installs. Once you are sure
  all instances of the codebase have applied the migrations you squashed,
  you can delete them.
</pre></div>
</div>
<p>Note that model interdependencies in Django can get very complex, and squashing
may result in migrations that do not run; either mis-optimized (in which case
you can try again with <code class="docutils literal"><span class="pre">--no-optimize</span></code>, though you should also report an issue),
or with a <code class="docutils literal"><span class="pre">CircularDependencyError</span></code>, in which case you can manually resolve it.</p>
<p>To manually resolve a <code class="docutils literal"><span class="pre">CircularDependencyError</span></code>, break out one of
the ForeignKeys in the circular dependency loop into a separate
migration, and move the dependency on the other app with it. If you&#8217;re unsure,
see how makemigrations deals with the problem when asked to create brand
new migrations from your models. In a future release of Django, squashmigrations
will be updated to attempt to resolve these errors itself.</p>
<p>Once you&#8217;ve squashed your migration, you should then commit it alongside the
migrations it replaces and distribute this change to all running instances
of your application, making sure that they run <code class="docutils literal"><span class="pre">migrate</span></code> to store the change
in their database.</p>
<p>After this has been done, you must then transition the squashed migration to
a normal initial migration, by:</p>
<ul class="simple">
<li>Deleting all the migration files it replaces</li>
<li>Removing the <code class="docutils literal"><span class="pre">replaces</span></code> argument in the <code class="docutils literal"><span class="pre">Migration</span></code> class of the
squashed migration (this is how Django tells that it is a squashed migration)</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Once you&#8217;ve squashed a migration, you should not then re-squash that squashed
migration until you have fully transitioned it to a normal migration.</p>
</div>
</div>
<div class="section" id="s-serializing-values">
<span id="s-migration-serializing"></span><span id="serializing-values"></span><span id="migration-serializing"></span><h2>Serializing values<a class="headerlink" href="#serializing-values" title="Permalink to this headline">¶</a></h2>
<p>Migrations are just Python files containing the old definitions of your models
- thus, to write them, Django must take the current state of your models and
serialize them out into a file.</p>
<p>While Django can serialize most things, there are some things that we just
can&#8217;t serialize out into a valid Python representation - there&#8217;s no Python
standard for how a value can be turned back into code (<code class="docutils literal"><span class="pre">repr()</span></code> only works
for basic values, and doesn&#8217;t specify import paths).</p>
<p>Django can serialize the following:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">int</span></code>, <code class="docutils literal"><span class="pre">long</span></code>, <code class="docutils literal"><span class="pre">float</span></code>, <code class="docutils literal"><span class="pre">bool</span></code>, <code class="docutils literal"><span class="pre">str</span></code>, <code class="docutils literal"><span class="pre">unicode</span></code>, <code class="docutils literal"><span class="pre">bytes</span></code>, <code class="docutils literal"><span class="pre">None</span></code></li>
<li><code class="docutils literal"><span class="pre">list</span></code>, <code class="docutils literal"><span class="pre">set</span></code>, <code class="docutils literal"><span class="pre">tuple</span></code>, <code class="docutils literal"><span class="pre">dict</span></code></li>
<li><code class="docutils literal"><span class="pre">datetime.date</span></code>, <code class="docutils literal"><span class="pre">datetime.time</span></code>, and <code class="docutils literal"><span class="pre">datetime.datetime</span></code> instances
(include those that are timezone-aware)</li>
<li><code class="docutils literal"><span class="pre">decimal.Decimal</span></code> instances</li>
<li>Any Django field</li>
<li>Any function or method reference (e.g. <code class="docutils literal"><span class="pre">datetime.datetime.today</span></code>) (must be in module&#8217;s top-level scope)</li>
<li>Any class reference (must be in module&#8217;s top-level scope)</li>
<li>Anything with a custom <code class="docutils literal"><span class="pre">deconstruct()</span></code> method (<a class="reference internal" href="#custom-deconstruct-method"><span class="std std-ref">see below</span></a>)</li>
</ul>
<div class="versionchanged">
<span class="title">Changed in Django 1.7.1:</span> <p>Support for serializing timezone-aware datetimes was added.</p>
</div>
<p>Django can serialize the following on Python 3 only:</p>
<ul class="simple">
<li>Unbound methods used from within the class body (see below)</li>
</ul>
<p>Django cannot serialize:</p>
<ul class="simple">
<li>Nested classes</li>
<li>Arbitrary class instances (e.g. <code class="docutils literal"><span class="pre">MyClass(4.3,</span> <span class="pre">5.7)</span></code>)</li>
<li>Lambdas</li>
</ul>
<p>Due to the fact <code class="docutils literal"><span class="pre">__qualname__</span></code> was only introduced in Python 3, Django can only
serialize the following pattern (an unbound method used within the class body)
on Python 3, and will fail to serialize a reference to it on Python 2:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">upload_to</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;something dynamic&quot;</span>

    <span class="n">my_file</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">(</span><span class="n">upload_to</span><span class="o">=</span><span class="n">upload_to</span><span class="p">)</span>
</pre></div>
</div>
<p>If you are using Python 2, we recommend you move your methods for upload_to
and similar arguments that accept callables (e.g. <code class="docutils literal"><span class="pre">default</span></code>) to live in
the main module body, rather than the class body.</p>
<div class="section" id="s-adding-a-deconstruct-method">
<span id="s-custom-deconstruct-method"></span><span id="adding-a-deconstruct-method"></span><span id="custom-deconstruct-method"></span><h3>Adding a deconstruct() method<a class="headerlink" href="#adding-a-deconstruct-method" title="Permalink to this headline">¶</a></h3>
<p>You can let Django serialize your own custom class instances by giving the class
a <code class="docutils literal"><span class="pre">deconstruct()</span></code> method. It takes no arguments, and should return a tuple
of three things <code class="docutils literal"><span class="pre">(path,</span> <span class="pre">args,</span> <span class="pre">kwargs)</span></code>:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">path</span></code> should be the Python path to the class, with the class name included
as the last part (for example, <code class="docutils literal"><span class="pre">myapp.custom_things.MyClass</span></code>). If your
class is not available at the top level of a module it is not serializable.</li>
<li><code class="docutils literal"><span class="pre">args</span></code> should be a list of positional arguments to pass to your class&#8217;
<code class="docutils literal"><span class="pre">__init__</span></code> method. Everything in this list should itself be serializable.</li>
<li><code class="docutils literal"><span class="pre">kwargs</span></code> should be a dict of keyword arguments to pass to your class&#8217;
<code class="docutils literal"><span class="pre">__init__</span></code> method. Every value should itself be serializable.</li>
</ul>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This return value is different from the <code class="docutils literal"><span class="pre">deconstruct()</span></code> method
<a class="reference internal" href="../howto/custom-model-fields.html#custom-field-deconstruct-method"><span class="std std-ref">for custom fields</span></a> which returns a
tuple of four items.</p>
</div>
<p>Django will write out the value as an instantiation of your class with the
given arguments, similar to the way it writes out references to Django fields.</p>
<p>To prevent a new migration from being created each time
<a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a> is run, you should also add a <code class="docutils literal"><span class="pre">__eq__()</span></code> method to
the decorated class. This function will be called by Django&#8217;s migration
framework to detect changes between states.</p>
<p>As long as all of the arguments to your class&#8217; constructor are themselves
serializable, you can use the <code class="docutils literal"><span class="pre">&#64;deconstructible</span></code> class decorator from
<code class="docutils literal"><span class="pre">django.utils.deconstruct</span></code> to add the <code class="docutils literal"><span class="pre">deconstruct()</span></code> method:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.utils.deconstruct</span> <span class="k">import</span> <span class="n">deconstructible</span>

<span class="nd">@deconstructible</span>
<span class="k">class</span> <span class="nc">MyCustomClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">foo</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">foo</span> <span class="o">=</span> <span class="n">foo</span>
        <span class="o">...</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">foo</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">foo</span>
</pre></div>
</div>
<p>The decorator adds logic to capture and preserve the arguments on their
way into your constructor, and then returns those arguments exactly when
deconstruct() is called.</p>
</div>
</div>
<div class="section" id="s-supporting-python-2-and-3">
<span id="supporting-python-2-and-3"></span><h2>Supporting Python 2 and 3<a class="headerlink" href="#supporting-python-2-and-3" title="Permalink to this headline">¶</a></h2>
<p>In order to generate migrations that support both Python 2 and 3, all string
literals used in your models and fields (e.g. <code class="docutils literal"><span class="pre">verbose_name</span></code>,
<code class="docutils literal"><span class="pre">related_name</span></code>, etc.), must be consistently either bytestrings or text
(unicode) strings in both Python 2 and 3 (rather than bytes in Python 2 and
text in Python 3, the default situation for unmarked string literals.)
Otherwise running <a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a> under Python 3 will generate
spurious new migrations to convert all these string attributes to text.</p>
<p>The easiest way to achieve this is to follow the advice in Django&#8217;s
<a class="reference internal" href="python3.html"><span class="doc">Python 3 porting guide</span></a> and make sure that all your
modules begin with <code class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">unicode_literals</span></code>, so that all
unmarked string literals are always unicode, regardless of Python version. When
you add this to an app with existing migrations generated on Python 2, your
next run of <a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a> on Python 3 will likely generate many
changes as it converts all the bytestring attributes to text strings; this is
normal and should only happen once.</p>
</div>
<div class="section" id="s-supporting-multiple-django-versions">
<span id="supporting-multiple-django-versions"></span><h2>Supporting multiple Django versions<a class="headerlink" href="#supporting-multiple-django-versions" title="Permalink to this headline">¶</a></h2>
<p>If you are the maintainer of a third-party app with models, you may need to
ship migrations that support multiple Django versions. In this case, you should
always run <a class="reference internal" href="../ref/django-admin.html#django-admin-makemigrations"><code class="xref std std-djadmin docutils literal"><span class="pre">makemigrations</span></code></a> <strong>with the lowest Django version you wish
to support</strong>.</p>
<p>The migrations system will maintain backwards-compatibility according to the
same policy as the rest of Django, so migration files generated on Django X.Y
should run unchanged on Django X.Y+1. The migrations system does not promise
forwards-compatibility, however. New features may be added, and migration files
generated with newer versions of Django may not work on older versions.</p>
</div>
<div class="section" id="s-upgrading-from-south">
<span id="s-id4"></span><span id="upgrading-from-south"></span><span id="id4"></span><h2>Upgrading from South<a class="headerlink" href="#upgrading-from-south" title="Permalink to this headline">¶</a></h2>
<p>If you already have pre-existing migrations created with
<a class="reference external" href="http://south.aeracode.org">South</a>, then the upgrade process to use
<code class="docutils literal"><span class="pre">django.db.migrations</span></code> is quite simple:</p>
<ul class="simple">
<li>Ensure all installs are fully up-to-date with their migrations.</li>
<li>Remove <code class="docutils literal"><span class="pre">'south'</span></code> from <a class="reference internal" href="../ref/settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a>.</li>
<li>Delete all your (numbered) migration files, but not the directory or
<code class="docutils literal"><span class="pre">__init__.py</span></code> - make sure you remove the <code class="docutils literal"><span class="pre">.pyc</span></code> files too.</li>
<li>Run <code class="docutils literal"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">makemigrations</span></code>. Django should see the empty
migration directories and make new initial migrations in the new format.</li>
<li>Run <code class="docutils literal"><span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">migrate</span> <span class="pre">--fake-initial</span></code>. Django will see that the
tables for the initial migrations already exist and mark them as applied
without running them. (Django won&#8217;t check that the table schema match your
models, just that the right table names exist).</li>
</ul>
<p>That&#8217;s it! The only complication is if you have a circular dependency loop
of foreign keys; in this case, <code class="docutils literal"><span class="pre">makemigrations</span></code> might make more than one
initial migration, and you&#8217;ll need to mark them all as applied using:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">manage</span><span class="o">.</span><span class="n">py</span> <span class="n">migrate</span> <span class="o">--</span><span class="n">fake</span> <span class="n">yourappnamehere</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <a class="reference internal" href="../ref/django-admin.html#django-admin-option---fake-initial"><code class="xref std std-djadminopt docutils literal"><span class="pre">--fake-initial</span></code></a> flag was added to <a class="reference internal" href="../ref/django-admin.html#django-admin-migrate"><code class="xref std std-djadmin docutils literal"><span class="pre">migrate</span></code></a>;
previously, initial migrations were always automatically fake-applied if
existing tables were detected.</p>
</div>
<div class="section" id="s-libraries-third-party-apps">
<span id="libraries-third-party-apps"></span><h3>Libraries/Third-party Apps<a class="headerlink" href="#libraries-third-party-apps" title="Permalink to this headline">¶</a></h3>
<p>If you are a library or app maintainer, and wish to support both South migrations
(for Django 1.6 and below) and Django migrations (for 1.7 and above) you should
keep two parallel migration sets in your app, one in each format.</p>
<p>To aid in this, South 1.0 will automatically look for South-format migrations
in a <code class="docutils literal"><span class="pre">south_migrations</span></code> directory first, before looking in <code class="docutils literal"><span class="pre">migrations</span></code>,
meaning that users&#8217; projects will transparently use the correct set as long
as you put your South migrations in the <code class="docutils literal"><span class="pre">south_migrations</span></code> directory and
your Django migrations in the <code class="docutils literal"><span class="pre">migrations</span></code> directory.</p>
<p>More information is available in the
<a class="reference external" href="http://south.readthedocs.org/en/latest/releasenotes/1.0.html#library-migration-path">South 1.0 release notes</a>.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference internal" href="../ref/migration-operations.html"><span class="doc">The Migrations Operations Reference</span></a></dt>
<dd>Covers the schema operations API, special operations, and writing your
own operations.</dd>
<dt><a class="reference internal" href="../howto/writing-migrations.html"><span class="doc">The Writing Migrations &#8220;how-to&#8221;</span></a></dt>
<dd>Explains how to structure and write database migrations for different
scenarios you might encounter.</dd>
</dl>
</div>
</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="#">Migrations</a><ul>
<li><a class="reference internal" href="#a-brief-history">A Brief History</a></li>
<li><a class="reference internal" href="#the-commands">The Commands</a></li>
<li><a class="reference internal" href="#backend-support">Backend Support</a><ul>
<li><a class="reference internal" href="#postgresql">PostgreSQL</a></li>
<li><a class="reference internal" href="#mysql">MySQL</a></li>
<li><a class="reference internal" href="#sqlite">SQLite</a></li>
</ul>
</li>
<li><a class="reference internal" href="#workflow">Workflow</a><ul>
<li><a class="reference internal" href="#version-control">Version control</a></li>
</ul>
</li>
<li><a class="reference internal" href="#dependencies">Dependencies</a></li>
<li><a class="reference internal" href="#migration-files">Migration files</a><ul>
<li><a class="reference internal" href="#custom-fields">Custom fields</a></li>
<li><a class="reference internal" href="#model-managers">Model managers</a></li>
</ul>
</li>
<li><a class="reference internal" href="#adding-migrations-to-apps">Adding migrations to apps</a></li>
<li><a class="reference internal" href="#historical-models">Historical models</a></li>
<li><a class="reference internal" href="#considerations-when-removing-model-fields">Considerations when removing model fields</a></li>
<li><a class="reference internal" href="#data-migrations">Data Migrations</a><ul>
<li><a class="reference internal" href="#accessing-models-from-other-apps">Accessing models from other apps</a></li>
<li><a class="reference internal" href="#more-advanced-migrations">More advanced migrations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#squashing-migrations">Squashing migrations</a></li>
<li><a class="reference internal" href="#serializing-values">Serializing values</a><ul>
<li><a class="reference internal" href="#adding-a-deconstruct-method">Adding a deconstruct() method</a></li>
</ul>
</li>
<li><a class="reference internal" href="#supporting-python-2-and-3">Supporting Python 2 and 3</a></li>
<li><a class="reference internal" href="#supporting-multiple-django-versions">Supporting multiple Django versions</a></li>
<li><a class="reference internal" href="#upgrading-from-south">Upgrading from South</a><ul>
<li><a class="reference internal" href="#libraries-third-party-apps">Libraries/Third-party Apps</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="class-based-views/mixins.html">Using mixins with class-based views</a></li>
    
    
      <li>Next: <a href="files.html">Managing files</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">Using Django</a>
        
        <ul><li>Migrations</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/topics/migrations.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="class-based-views/mixins.html" title="Using mixins with class-based views">previous</a>
     |
    <a href="index.html" title="Using Django" accesskey="U">up</a>
   |
    <a href="files.html" title="Managing files">next</a> &raquo;</div>
    </div>
  </div>

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