python - 如何将数据库路由器添加到 Django 项目

标签 python django module django-database

我正在按照此处 topics/db/multi-db 中有关如何处理一个 Django 项目中的多个数据库的说明进行操作。

我已经创建了所需的两个路由器。 它们保存为 ./database_routers/discourse.py 和 ./database_routers/wordpress.py

./database_routers/discourse.py 的内容是

class DiscourseRouter:
    """
    A router to control all database operations on models in the
    discourse application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read discourse models go to discourse.
        """
        if model._meta.app_label == 'discourse':
            return 'discourse'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write discourse models go to discourse.
        """
        if model._meta.app_label == 'discourse':
            return 'discourse'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the discourse app is involved.
        """
        if obj1._meta.app_label == 'discourse' or \
           obj2._meta.app_label == 'discourse':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the discourse app only appears in the 'discourse'
        database.
        """
        if app_label == 'discourse':
            return db == 'discourse'
        return None

./database_routers/wordpress.py 的内容是

class WordpressRouter:
    """
    A router to control all database operations on models in the
    wordpress application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read wordpress models go to wordpress.
        """
        if model._meta.app_label == 'wordpress':
            return 'wordpress'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write wordpress models go to wordpress.
        """
        if model._meta.app_label == 'wordpress':
            return 'wordpress'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the wordpress app is involved.
        """
        if obj1._meta.app_label == 'wordpress' or \
           obj2._meta.app_label == 'wordpress':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the wordpress app only appears in the 'wordpress'
        database.
        """
        if app_label == 'wordpress':
            return db == 'wordpress'
        return None

我创建了一个空的./database_routers/__init__.py文件

我在 api/settings 中设置的数据库路由器设置

DATABASE_ROUTERS = ['database_routers.DiscourseRouter', 'database_routers.WordpressRouter']

当我尝试使用 shell 和 I 查看项目时

 ./manage.py shell_plus

我明白

ImportError: Module "database_routers" does not define a "DiscourseRouter" attribute/class

如何将数据库路由器添加到 Django 项目中,以便 python 识别路径directory_name.ClassName?

最佳答案

您错过了模块名称。

DATABASE_ROUTERS = [
    'database_routers.discourse.DiscourseRouter', 
    'database_routers.wordpress.WordpressRouter'
]

关于python - 如何将数据库路由器添加到 Django 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53859629/

相关文章:

python - "I/O operation on closed file"在 Django View 测试中使用 StringIO

python - 导入错误 : No module named 'django.core.urlresolvers'

python - 如何对第二列定义的固定间隔内一列的元素求和?

python - 如何在 django 的 sql 查询中进行一般数学运算?

mysql - 找不到模块 mysql - 错误

python - 如何解决Python中的操作系统模块路径错误

ios - 为什么在将现有的原生项目与flutter代码结合时使用模块模板?

python - 使用列表理解在 Python 中设置唯一字典列表

python - 如何在 Python 中正确地将双引号传递给 awk 子进程?

python - Django:跨三个模型进行注释