Update a model field – Django

I noticed a typo in a field name after running migrations, I followed these steps to update the field without loosing data.

  1. Update the filed name in the model with the desired name (taking note of the current name)
  2. Create an empty migration. If you have previous migrations, this one must be named in such a way that it comes last in the migrations list. eg if you already have 001_users and 002_emails then this new migration needs to be named 003_new_name

The new migration should look like this:

from __future__ import unicode_literals

from django.db import migrations
class Migration(migrations.Migration):

dependencies = [
('django_app_name', 'last_previous_migration'), # last_previous_migration is the migration immediately before this new empty migration without the .py extension

operations = [
old_name='old_filed_name', #that was taken note of from step one above
new_name='description', #the new/desired field name

3. run migrations

 $ python migrate 

This will update the field.

Hope this saves someone some googling time


Leave a Reply

Your email address will not be published. Required fields are marked *