Renaming models(tables) in Django

16.6k views Asked by At

so I've already created models in Django for my db, but now want to rename the model. I've change the names in the Meta class and then make migrations/migrate but that just creates brand new tables.

I've also tried schemamigration but also not working, I'm using Django 1.7

Here's my model

class ResultType(models.Model):
    name = models.CharField(max_length=150)
    ut = models.DateTimeField(default=datetime.now)
    class Meta:
       db_table = u'result_type'

    def __unicode__(self):
        return self.name

Cheers

1

There are 1 answers

8
coldmind On

Django does not know, what you are trying to do. By default it will delete old table and create new. You need to create an empty migration, then use this operation (you need to write it by yourself):

https://docs.djangoproject.com/en/stable/ref/migration-operations/#renamemodel

Something like this:

from django.db import migrations

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RenameModel("OldName", "NewName")
    ]