Renaming a Model and migrating the data using South datamigration
OMG What a ball-ache. The whole of my saturday wasted on figuring out a simple way to rename a model. Due to the many tables and sequences created in postgres, every attempt to rename a django model failed. Finally I discovered South's datamigration option.
All I wanted was to rename a model called Student to Customer. Amazingly simple but complicated. Simplication. I created the extra Customer model in models.py, migrated the schema, the created a datamigration (read the South docs) and edited it as below.
After applying this migration I removed the Student model from models.py, changed any linking fields in other models from Student to Customer and migrated again to delete the Student model/tables.
It's probably a lot more complicated with M2M fields but I'll cross that bridge when I come to it. Inelegant but simple.
// edited migration file for datamigration class Migration(DataMigration): def forwards(self, orm): "Write your forwards methods here." for student in orm.Student.objects.all(): customer,result = orm.Customer.objects.get_or_create( id=student.id) customer.first_name = student.first_name customer.middle_name = student.middle_name customer.last_name = student.last_name customer.show_middle_name = student.show_middle_name customer.email = student.email customer.applied = student.applied customer.signup_date = student.signup_date customer.active = student.active customer.save() def backwards(self, orm): raise RuntimeError("Cannot reverse this migration.")














