Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming models(tables) in Django

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

like image 680
Toby Green Avatar asked Nov 27 '14 16:11

Toby Green


People also ask

How do I change models in Django?

Create or update a model. Run ./manage.py makemigrations <app_name> Run ./manage.py migrate to migrate everything or ./manage.py migrate <app_name> to migrate an individual app. Repeat as necessary.

What does class Meta do in Django?

Model Meta is basically the inner class of your model class. Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name, and a lot of other options. It's completely optional to add a Meta class to your model.


1 Answers

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")
    ]
like image 198
coldmind Avatar answered Oct 11 '22 21:10

coldmind