在Django中重命名模型(表) [英] Renaming models(tables) in Django

查看:270
本文介绍了在Django中重命名模型(表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经在Django中为我的数据库创建了模型,但是现在想重命名模型.我已经更改了Meta类中的名称,然后进行了迁移/迁移,但这只是创建了全新的表.

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.

我也尝试了schemamigration但也没有用,我使用的是Django 1.7

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

这是我的模特

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

欢呼

推荐答案

Django不知道您要做什么.默认情况下,它将删除旧表并创建新表. 您需要创建一个空迁移,然后使用此操作(您需要自己编写):

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

类似这样的东西:

class Migration(migrations.Migration):

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

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

这篇关于在Django中重命名模型(表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆