Sophie

Sophie

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

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>SchemaEditor &#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="API Reference" href="index.html" />
    <link rel="next" title="Settings" href="settings.html" />
    <link rel="prev" title="Request and response objects" href="request-response.html" />



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


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

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.8.19 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="request-response.html" title="Request and response objects">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="settings.html" title="Settings">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-schema-editor">
            
  <div class="section" id="s-module-django.db.backends.base.schema">
<span id="s-schemaeditor"></span><span id="module-django.db.backends.base.schema"></span><span id="schemaeditor"></span><h1><code class="docutils literal"><span class="pre">SchemaEditor</span></code><a class="headerlink" href="#module-django.db.backends.base.schema" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor">
<em class="property">class </em><code class="descname">BaseDatabaseSchemaEditor</code><a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Django&#8217;s migration system is split into two parts; the logic for calculating
and storing what operations should be run (<code class="docutils literal"><span class="pre">django.db.migrations</span></code>), and the
database abstraction layer that turns things like &#8220;create a model&#8221; or
&#8220;delete a field&#8221; into SQL - which is the job of the <code class="docutils literal"><span class="pre">SchemaEditor</span></code>.</p>
<p>It&#8217;s unlikely that you will want to interact directly with <code class="docutils literal"><span class="pre">SchemaEditor</span></code> as
a normal developer using Django, but if you want to write your own migration
system, or have more advanced needs, it&#8217;s a lot nicer than writing SQL.</p>
<p>Each database backend in Django supplies its own version of <code class="docutils literal"><span class="pre">SchemaEditor</span></code>,
and it&#8217;s always accessible via the <code class="docutils literal"><span class="pre">connection.schema_editor()</span></code> context
manager:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">connection</span><span class="o">.</span><span class="n">schema_editor</span><span class="p">()</span> <span class="k">as</span> <span class="n">schema_editor</span><span class="p">:</span>
    <span class="n">schema_editor</span><span class="o">.</span><span class="n">delete_model</span><span class="p">(</span><span class="n">MyModel</span><span class="p">)</span>
</pre></div>
</div>
<p>It must be used via the context manager as this allows it to manage things
like transactions and deferred SQL (like creating <code class="docutils literal"><span class="pre">ForeignKey</span></code> constraints).</p>
<p>It exposes all possible operations as methods, that should be called in
the order you wish changes to be applied. Some possible operations or types
of change are not possible on all databases - for example, MyISAM does not
support foreign key constraints.</p>
<p>If you are writing or maintaining a third-party database backend for Django,
you will need to provide a <code class="docutils literal"><span class="pre">SchemaEditor</span></code> implementation in order to work with
1.7&#8217;s migration functionality - however, as long as your database is relatively
standard in its use of SQL and relational design, you should be able to
subclass one of the built-in Django <code class="docutils literal"><span class="pre">SchemaEditor</span></code> classes and just tweak the
syntax a little. Also note that there are a few new database features that
migrations will look for: <code class="docutils literal"><span class="pre">can_rollback_ddl</span></code> and
<code class="docutils literal"><span class="pre">supports_combined_alters</span></code> are the most important.</p>
<div class="section" id="s-methods">
<span id="methods"></span><h2>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h2>
<div class="section" id="s-execute">
<span id="execute"></span><h3>execute<a class="headerlink" href="#execute" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">execute</code>(<em>sql</em>, <em>params=[]</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.execute"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Executes the SQL statement passed in, with parameters if supplied. This
is a simple wrapper around the normal database cursors that allows
capture of the SQL to a <code class="docutils literal"><span class="pre">.sql</span></code> file if the user wishes.</p>
</div>
<div class="section" id="s-create-model">
<span id="create-model"></span><h3>create_model<a class="headerlink" href="#create-model" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">create_model</code>(<em>model</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.create_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Creates a new table in the database for the provided model, along with any
unique constraints or indexes it requires.</p>
</div>
<div class="section" id="s-delete-model">
<span id="delete-model"></span><h3>delete_model<a class="headerlink" href="#delete-model" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">delete_model</code>(<em>model</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.delete_model"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Drops the model&#8217;s table in the database along with any unique constraints
or indexes it has.</p>
</div>
<div class="section" id="s-alter-unique-together">
<span id="alter-unique-together"></span><h3>alter_unique_together<a class="headerlink" href="#alter-unique-together" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_unique_together">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">alter_unique_together</code>(<em>model</em>, <em>old_unique_together</em>, <em>new_unique_together</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.alter_unique_together"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_unique_together" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Changes a model&#8217;s <a class="reference internal" href="models/options.html#django.db.models.Options.unique_together" title="django.db.models.Options.unique_together"><code class="xref py py-attr docutils literal"><span class="pre">unique_together</span></code></a> value; this
will add or remove unique constraints from the model&#8217;s table until they match
the new value.</p>
</div>
<div class="section" id="s-alter-index-together">
<span id="alter-index-together"></span><h3>alter_index_together<a class="headerlink" href="#alter-index-together" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_index_together">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">alter_index_together</code>(<em>model</em>, <em>old_index_together</em>, <em>new_index_together</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.alter_index_together"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_index_together" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Changes a model&#8217;s <a class="reference internal" href="models/options.html#django.db.models.Options.index_together" title="django.db.models.Options.index_together"><code class="xref py py-attr docutils literal"><span class="pre">index_together</span></code></a> value; this
will add or remove indexes from the model&#8217;s table until they match the new
value.</p>
</div>
<div class="section" id="s-alter-db-table">
<span id="alter-db-table"></span><h3>alter_db_table<a class="headerlink" href="#alter-db-table" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_table">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">alter_db_table</code>(<em>model</em>, <em>old_db_table</em>, <em>new_db_table</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.alter_db_table"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_table" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Renames the model&#8217;s table from <code class="docutils literal"><span class="pre">old_db_table</span></code> to <code class="docutils literal"><span class="pre">new_db_table</span></code>.</p>
</div>
<div class="section" id="s-alter-db-tablespace">
<span id="alter-db-tablespace"></span><h3>alter_db_tablespace<a class="headerlink" href="#alter-db-tablespace" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_tablespace">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">alter_db_tablespace</code>(<em>model</em>, <em>old_db_tablespace</em>, <em>new_db_tablespace</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.alter_db_tablespace"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_tablespace" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Moves the model&#8217;s table from one tablespace to another.</p>
</div>
<div class="section" id="s-add-field">
<span id="add-field"></span><h3>add_field<a class="headerlink" href="#add-field" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">add_field</code>(<em>model</em>, <em>field</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.add_field"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Adds a column (or sometimes multiple) to the model&#8217;s table to represent the
field. This will also add indexes or a unique constraint
if the field has <code class="docutils literal"><span class="pre">db_index=True</span></code> or <code class="docutils literal"><span class="pre">unique=True</span></code>.</p>
<p>If the field is a <code class="docutils literal"><span class="pre">ManyToManyField</span></code> without a value for <code class="docutils literal"><span class="pre">through</span></code>, instead
of creating a column, it will make a table to represent the relationship. If
<code class="docutils literal"><span class="pre">through</span></code> is provided, it is a no-op.</p>
<p>If the field is a <code class="docutils literal"><span class="pre">ForeignKey</span></code>, this will also add the foreign key
constraint to the column.</p>
</div>
<div class="section" id="s-remove-field">
<span id="remove-field"></span><h3>remove_field<a class="headerlink" href="#remove-field" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">remove_field</code>(<em>model</em>, <em>field</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.remove_field"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Removes the column(s) representing the field from the model&#8217;s table, along
with any unique constraints, foreign key constraints, or indexes caused by
that field.</p>
<p>If the field is a ManyToManyField without a value for <code class="docutils literal"><span class="pre">through</span></code>, it will
remove the table created to track the relationship. If
<code class="docutils literal"><span class="pre">through</span></code> is provided, it is a no-op.</p>
</div>
<div class="section" id="s-alter-field">
<span id="alter-field"></span><h3>alter_field<a class="headerlink" href="#alter-field" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_field">
<code class="descclassname">BaseDatabaseSchemaEditor.</code><code class="descname">alter_field</code>(<em>model</em>, <em>old_field</em>, <em>new_field</em>, <em>strict=False</em>)<a class="reference internal" href="../_modules/django/db/backends/base/schema.html#BaseDatabaseSchemaEditor.alter_field"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_field" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This transforms the field on the model from the old field to the new one. This
includes changing the name of the column (the
<a class="reference internal" href="models/fields.html#django.db.models.Field.db_column" title="django.db.models.Field.db_column"><code class="xref py py-attr docutils literal"><span class="pre">db_column</span></code></a> attribute), changing the type of the
field (if the field class changes), changing the <code class="docutils literal"><span class="pre">NULL</span></code> status of the field,
adding or removing field-only unique constraints and indexes, changing primary
key, and changing the destination of <code class="docutils literal"><span class="pre">ForeignKey</span></code> constraints.</p>
<p>The most common transformation this cannot do is transforming a
<code class="docutils literal"><span class="pre">ManyToManyField</span></code> into a normal Field or vice-versa; Django cannot do this
without losing data, and so it will refuse to do it. Instead,
<a class="reference internal" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field" title="django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field"><code class="xref py py-meth docutils literal"><span class="pre">remove_field()</span></code></a> and <a class="reference internal" href="#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field" title="django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field"><code class="xref py py-meth docutils literal"><span class="pre">add_field()</span></code></a> should be called separately.</p>
<p>If the database has the <code class="docutils literal"><span class="pre">supports_combined_alters</span></code>, Django will try and
do as many of these in a single database call as possible; otherwise, it will
issue a separate ALTER statement for each change, but will not issue ALTERs
where no change is required (as South often did).</p>
</div>
</div>
<div class="section" id="s-attributes">
<span id="attributes"></span><h2>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h2>
<p>All attributes should be considered read-only unless stated otherwise.</p>
<div class="section" id="s-connection">
<span id="connection"></span><h3>connection<a class="headerlink" href="#connection" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.backends.base.schema.SchemaEditor.connection">
<code class="descclassname">SchemaEditor.</code><code class="descname">connection</code><a class="headerlink" href="#django.db.backends.base.schema.SchemaEditor.connection" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A connection object to the database. A useful attribute of the connection is
<code class="docutils literal"><span class="pre">alias</span></code> which can be used to determine the name of the database being
accessed.</p>
<p>This is useful when doing data migrations for <a class="reference internal" href="../howto/writing-migrations.html#data-migrations-and-multiple-databases"><span class="std std-ref">migrations with multiple
databases</span></a>.</p>
</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="#"><code class="docutils literal"><span class="pre">SchemaEditor</span></code></a><ul>
<li><a class="reference internal" href="#methods">Methods</a><ul>
<li><a class="reference internal" href="#execute">execute</a></li>
<li><a class="reference internal" href="#create-model">create_model</a></li>
<li><a class="reference internal" href="#delete-model">delete_model</a></li>
<li><a class="reference internal" href="#alter-unique-together">alter_unique_together</a></li>
<li><a class="reference internal" href="#alter-index-together">alter_index_together</a></li>
<li><a class="reference internal" href="#alter-db-table">alter_db_table</a></li>
<li><a class="reference internal" href="#alter-db-tablespace">alter_db_tablespace</a></li>
<li><a class="reference internal" href="#add-field">add_field</a></li>
<li><a class="reference internal" href="#remove-field">remove_field</a></li>
<li><a class="reference internal" href="#alter-field">alter_field</a></li>
</ul>
</li>
<li><a class="reference internal" href="#attributes">Attributes</a><ul>
<li><a class="reference internal" href="#connection">connection</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="request-response.html">Request and response objects</a></li>
    
    
      <li>Next: <a href="settings.html">Settings</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><code class="docutils literal"><span class="pre">SchemaEditor</span></code></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/schema-editor.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="request-response.html" title="Request and response objects">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="settings.html" title="Settings">next</a> &raquo;</div>
    </div>
  </div>

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