232
You can use the db.rename_column
function.
class Migration:
def forwards(self, orm):
# Rename 'name' field to 'full_name'
db.rename_column('app_foo', 'name', 'full_name')
def backwards(self, orm):
# Rename 'full_name' field to 'name'
db.rename_column('app_foo', 'full_name', 'name')
The first argument of db.rename_column
is the table name, so it’s important to remember how Django creates table names:
Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model’s database table name is constructed by joining the model’s “app label” — the name you used in manage.py startapp — to the model’s class name, with an underscore between them.
In the case where you have a multi-worded, camel-cased model name, such as ProjectItem, the table name will be app_projectitem
(i.e., an underscore will not be inserted between project
and item
even though they are camel-cased).
39
Here’s what I do:
- Make the column name change in your model (in this example it would be
myapp/models.py
) - Run
./manage.py schemamigration myapp renaming_column_x --auto
Note renaming_column_x
can be anything you like, it’s just a way of giving a descriptive name to the migration file.
This will generate you a file called myapp/migrations/000x_renaming_column_x.py
which will delete your old column and add a new column.
Modify the code in this file to change the migration behaviour to a simple rename:
class Migration(SchemaMigration):
def forwards(self, orm):
# Renaming column 'mymodel.old_column_name' to 'mymodel.new_column_name'
db.rename_column(u'myapp_mymodel', 'old_column_name', 'new_column_name')
def backwards(self, orm):
# Renaming column 'mymodel.new_column_name' to 'mymodel.old_column_name'
db.rename_column(u'myapp_mymodel', 'new_column_name', 'old_column_name')
- [Django]-Charts in django Web Applications
- [Django]-Multiple Database Config in Django 1.2
- [Django]-Django ignores router when running tests?
15
I didn’t know about db.rename column, sounds handy, however in the past I have added the new column as one schemamigration, then created a datamigration to move values into the new field, then a second schemamigration to remove the old column
- [Django]-How do I run tests against a Django data migration?
- [Django]-How do I create a slug in Django?
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
12
Django 1.7 introduced Migrations so now you don’t even need to install extra package to manage your migrations.
To rename your model you need to create empty migration first:
$ manage.py makemigrations <app_name> --empty
Then you need to edit your migration’s code like this:
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('yourapp', 'XXXX_your_previous_migration'),
]
operations = [
migrations.RenameField(
model_name='Foo',
old_name='name',
new_name='full_name'
),
migrations.RenameField(
model_name='Foo',
old_name='rel',
new_name='odd_relation'
),
]
And after that you need to run:
$ manage.py migrate <app_name>
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-Numeric for loop in Django templates
7
Just change the model and run makemigrations
in 1.9
Django automatically detects that you’ve deleted and created a single field, and asks:
Did you rename model.old to model.new (a IntegerField)? [y/N]
Say yes, and the right migration gets created. Magic.
- [Django]-Embedding JSON objects in script tags
- [Django]-Best practice for Django project working directory structure
- [Django]-What is related_name used for?
0
- Add
south
to your installed apps in project setting file. - Comment out the added/modified field/table.
$ manage.py Schemamigration <app_name> --initial
$ manage.py migrate <app_name> --Fake
- Un-comment the field and write the modified one
$ manage.py Schemamigration --auto
$ manage.py migrate <app_name>
If you are using ‘pycharm’, then you can use ‘ctrl+shift+r’ instead of ‘manage.py’ , and ‘shift ‘ for parameters.
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-Django render_to_string missing information