Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rename field using migrations in django 1.7
To change a field name in django 1.7+
1. Edit the field name in the model (but remember the old field name: you need it for step 3!)
2. Create an empty migration
$ python manage.py makemigrations --empty myApp
3. Edit the empty migration (it's in the migrations folder in your app folder, and will be the most recent migration) by adding
migrations.RenameField('MyModel', 'old_field_name', 'new_field_name'),
to the operations list.
4. Apply the migration
$ python manage.py migrate myApp
@citadelgrad

This comment has been minimized.

Copy link

@citadelgrad citadelgrad commented Mar 30, 2016

Thanks for this!

@vangale

This comment has been minimized.

Copy link

@vangale vangale commented May 31, 2016

Thanks Doug! 🎱

@digitalmacgyver

This comment has been minimized.

Copy link

@digitalmacgyver digitalmacgyver commented Jul 2, 2017

If this were on Stack Overflow you'd have tons of reputation - thanks!

@milinmestry

This comment has been minimized.

Copy link

@milinmestry milinmestry commented Aug 7, 2017

Thanks a ton!

@jeromegit

This comment has been minimized.

Copy link

@jeromegit jeromegit commented Nov 19, 2017

Exactly what I needed and it worked perfectly.
Thanks, Doug!

@HoverHell

This comment has been minimized.

Copy link

@HoverHell HoverHell commented Mar 30, 2018

  1. Create an empty migration

It is a bit easier to create an automatic migration, and replace the RemoveField + AddField pair with one RenameField (or with RenameField + AlterField if needed).

@ivancarrancho

This comment has been minimized.

Copy link

@ivancarrancho ivancarrancho commented Aug 6, 2018

operations = [ migrations.RenameField('MyModel', 'old_field_name', 'new_field_name'), ]
perfect!

@amirkarimi

This comment has been minimized.

Copy link

@amirkarimi amirkarimi commented Dec 29, 2018

I'm not sure when this feature is added to Django but just accidentally found Django 2.1.4 asks for renames when running makemigrations. At least in my case, which was location_state -> location_city.

@ocomsoft

This comment has been minimized.

Copy link

@ocomsoft ocomsoft commented Mar 20, 2019

@amirkarimi I found this doesn't always happen - I came here from a google search because I needed to change a migration that dropped the field and added the new one instead of renaming it.

@vanessaLatefa

This comment has been minimized.

Copy link

@vanessaLatefa vanessaLatefa commented Sep 11, 2019

HERO!!!

@MichelML

This comment has been minimized.

Copy link

@MichelML MichelML commented Apr 22, 2020

thanks!

@SoniaStalance

This comment has been minimized.

Copy link

@SoniaStalance SoniaStalance commented May 26, 2020

Thankyou so much!

@klchanhenry

This comment has been minimized.

Copy link

@klchanhenry klchanhenry commented Aug 2, 2020

Great! Working !

@bradicalone

This comment has been minimized.

Copy link

@bradicalone bradicalone commented Aug 6, 2020

Easier way I found:
1st: Is changing the model to the correct name you want.
2nd: Changing the file in migrations folder which usually looks like 0001_initial.py and changing the field name in the list to the correct field name you want also.
3rd: change the column name in the Database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment