Sometimes, we want to rename a model and relationship fields with Python Django migrations.
In this article, we’ll look at how to rename a model and relationship fields with Python Django migrations.
How to rename a model and relationship fields with Python Django migrations?
To rename a model and relationship fields with Python Django migrations, we can use the RenameModel
and RenameField
methods.
For instance, we write
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.RenameModel('Foo', 'Bar'),
migrations.RenameField('AnotherModel', 'foo', 'bar')
]
to create the Migration
migration class that has the operations
list.
We call RenameModel
to rename a model field’s name from Foo
to Bar
.
And we call RenameField
to rename the related fields from 'foo'
to 'bar'
.
Then we run manage.py makemigrations
to run the migration.
Conclusion
To rename a model and relationship fields with Python Django migrations, we can use the RenameModel
and RenameField
methods.