python - 重命名在另一个模型中用作超类的模型
问题描述
我有一个FirstName
需要重命名为的SecondName
类,但该类也用作其他模型的基类。
# file 1
class FirstName(models.Model):
pass # stuff
# file 2
from firstfile.models import FirstName
class ProxyModel(FirstName):
pass # stuff
我尝试更新模型名称以及对它的所有引用...
# file 1
class SecondName(models.Model):
pass # stuff
# file 2
from firstfile.models import SecondName
class ProxyModel(SecondName):
pass # stuff
# migration
operations = [
migrations.RenameModel(
old_name='FirstName',
new_name='SecondName',
),
]
但是当我运行makemigrations
或“迁移”时出现错误:
django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'my_app.ProxyModel'>] This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth) in an app with no migrations;
似乎我无法在迁移之前导入重命名的基本模型,但在解决基类问题之前我无法迁移......
我已经尝试使用以前的模型名称作为 Proxy 基类,但得到模型未找到错误。我还尝试注释掉代理模型,但仍然出现错误(我认为是因为该代理模型已经运行了迁移)。
我怎样才能绕过这个重命名依赖循环?
Python 2.7、Django 1.11
解决方案
为什么不使用db_table
元属性而不是重命名数据库中的表?
class SecondName(models.Model):
pass # stuff
class Meta:
db_table = 'app_firstname'
推荐阅读
- python - 设置和获取命令如何在 python 中工作
- javascript - 正确渲染来自 API 的重音字符
- tensorflow - 如何在 tensorflow 2.0 中打印符号张量的值?
- flutter - 根据 CupertinoTextField 是否为空启用/禁用 CupertinoDialogAction
- c++ - 如何自定义clang的包含路径顺序?
- c# - C# - WPF - MVVM - 在 ItemsControl 中绑定自定义用户控件
- c++ - C++ 游戏出现错误,我很确定指针有问题。但我不能告诉
- javascript - 将 datalist 中的多个选定项目附加到带逗号的输入标签
- r - 如何从R中的文件中绘制累积数图?
- http - 如何监听 ipfs 网关后面的 HTTP 发布请求?