设置:
Django 1.7 | Postgres 9.x版
class Buildings(BaseModel):
number = models.CharField(max_length=25)
class TestGeneric(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
假设我创建了一个TestGeneric
实例,将其与Building
关联并保存了它:
现在我将Buildings
重命名为Building
,然后运行makemigrations
。我被提示Did you rename the app.Buildings model to Building? [y/N]
我选择是的。然后我运行migrate
,得到:
The following content types are stale and need to be deleted:
app | buildings
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.
不管我回答什么,Django将自动在django_content_type
中创建一个新行,名称和标签为building
。有没有办法只重命名ContentType
,这样我所有的TestGeneric行都不会被吹走?在
我刚刚在一个项目中使用了这个方法;警告是,如果您在尝试应用自动创建的模型重命名迁移之前创建了迁移,那么这个方法是可以正常工作的。在
您需要更改应用程序名称、模型名称和以前的迁移以匹配您的设置;在本例中,我们将模型的名称从
profile
更改为member
。在相关问题 更多 >
编程相关推荐