django - 在 django 应用程序中获取所有模型名称时出错
问题描述
我在我的 django 应用程序中使用不同的数据库,而数据库路由时,我试图让所有模型使用
all_models = dict([(name.lower(), cls) for name, cls in app.models.__dict__.items() \
if isinstance(cls, type)])
在运行迁移时,我收到错误“未定义名称应用程序”。
这是我的 routers.py
from .models import *
allmodels = dict([(name.lower(), cls) for name, cls in
app.models.__dict__.items() \
if isinstance(cls, type)])
class MyDBRouter(object):
def db_for_read(self, model, **hints):
""" reading model based on params """
return getattr(model.params, 'db')
def db_for_write(self, model, **hints):
""" writing model based on params """
return getattr(model.params, 'db')
def allow_migrate(self, db, app_label, model_name = None, **hints):
""" migrate to appropriate database per model """
model = allmodels.get(model_name)
return(model.params.db == db)
Settings.py https://pastebin.com/0XdpqxkJ
解决方案
推荐阅读
- python - numpy 的 argpartition 如何在文档的示例中起作用?
- multithreading - JMeter: How do I inject specific range of values in 2 different parameters of http requests
- python-3.x - Why does Pygame NEED an event handler to function?
- c# - datagridview中的到期日期需要通知
- swift - 从 Firebase 获取多级节点
- c# - 无法从 Json C# 获取特定字符串
- android - 如何在android studio中获取applicationid?
- laravel - 如何使用 Laravel Rinvex 属性?
- python - 如何在python中生成唯一模值的随机数
- c++ - 调试断言失败 C++:向量下标超出范围