Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>How to migrate using south.</h2> <p>Lets say we got two apps: common and specific:</p> <pre class="lang-none prettyprint-override"><code>myproject/ |-- common | |-- migrations | | |-- 0001_initial.py | | `-- 0002_create_cat.py | `-- models.py `-- specific |-- migrations | |-- 0001_initial.py | `-- 0002_create_dog.py `-- models.py </code></pre> <p>Now we want to move model common.models.cat to specific app (precisely to specific.models.cat). First make the changes in the source code and then run:</p> <pre class="lang-none prettyprint-override"><code>$ python manage.py schemamigration specific create_cat --auto + Added model 'specific.cat' $ python manage.py schemamigration common drop_cat --auto - Deleted model 'common.cat' myproject/ |-- common | |-- migrations | | |-- 0001_initial.py | | |-- 0002_create_cat.py | | `-- 0003_drop_cat.py | `-- models.py `-- specific |-- migrations | |-- 0001_initial.py | |-- 0002_create_dog.py | `-- 0003_create_cat.py `-- models.py </code></pre> <p>Now we need to edit both migration files:</p> <pre class="lang-py prettyprint-override"><code>#0003_create_cat: replace existing forward and backward code #to use just one sentence: def forwards(self, orm): db.rename_table('common_cat', 'specific_cat') if not db.dry_run: # For permissions to work properly after migrating orm['contenttypes.contenttype'].objects.filter( app_label='common', model='cat', ).update(app_label='specific') def backwards(self, orm): db.rename_table('specific_cat', 'common_cat') if not db.dry_run: # For permissions to work properly after migrating orm['contenttypes.contenttype'].objects.filter( app_label='specific', model='cat', ).update(app_label='common') </code></pre> <hr> <pre class="lang-py prettyprint-override"><code>#0003_drop_cat:replace existing forward and backward code #to use just one sentence; add dependency: depends_on = ( ('specific', '0003_create_cat'), ) def forwards(self, orm): pass def backwards(self, orm): pass </code></pre> <p>Now both apps migrations are aware of the change and life sucks just a little less :-) Setting this relationship between migrations is key of success. Now if you do:</p> <pre class="lang-none prettyprint-override"><code>python manage.py migrate common &gt; specific: 0003_create_cat &gt; common: 0003_drop_cat </code></pre> <p>will do both migration, and </p> <pre class="lang-none prettyprint-override"><code>python manage.py migrate specific 0002_create_dog &lt; common: 0003_drop_cat &lt; specific: 0003_create_cat </code></pre> <p>will migrate things down.</p> <p><strong>Notice that for upgrading of schema I used common app and for downgrading, I used specific app. That's because how the dependency here works.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload