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

2024-03-30

我正在按照此处有关如何在一个 Django 项目中处理多个数据库的说明进行操作主题/数据库/多数据库 https://docs.djangoproject.com/en/2.1/topics/db/multi-db/

我已经创建了所需的两个路由器。 它们保存为 ./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 file

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

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

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

 ./manage.py shell_plus

I get

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'
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将数据库路由器添加到 Django 项目 的相关文章

随机推荐

  • shell32 copyhere 在.Net 和 powershell 脚本中都不起作用

    有一个 powershell 脚本可以通过 MTP 以编程方式在 PC 和 Android 之间复制文件 请参阅针对 mtp 连接访问文件系统 http www pstips net access file system against m
  • PHP GET 变量数组注入

    我最近了解到可以将数组注入 PHP GET 变量来执行代码 php a asd a asdasd b a 这就是我得到的例子 我不知道它是如何工作的 并且想知道这是否可能 PHP 将解析查询字符串 并将这些值注入到 GET超全局数组 同样适
  • 如何在 VB NET 中读取控制台缓冲区?

    我见过一个使用 ConsoleRead API 函数的 C 示例 但是当我尝试将其转换为 VBNET 时 出现了很多错误 在其他网站 如 pinvoke 中 独特的示例也是针对 C 的 我找不到任何好的示例ConsoleRead API 函
  • 可展开的树形菜单列表

    我想要实现的目标如下 我想要一个树视图列表 它将出现在页面的左侧 当展开一个项目时 它的项目是从数据库加载的 我可以做这部分 所以基本上它会像这样 Category1 Category2 Category3 Category4 当用户点击
  • 我可以让 clang 对 Objective-C 文字对象使用自定义类吗?

    gcc 文档参考了 fconstant string class http gcc gnu org onlinedocs gcc Constant string objects html参数 它允许您更改用于的类 Objective C s
  • PrintWriter 未打印出完整的字符串

    我有以下代码 FileWriter F new FileWriter out txt PrintWriter H new PrintWriter F H print split split length 2 H print END 然而 当
  • 如何在 TinyMCE 中允许自闭合标签?

    我的视频元素内的源标签需要自动关闭 这样 iPad 才不会开始抱怨 我输入
  • 如何在 android 的 logcat 中查看数组

    我想在 logcat 中记录原始数据 例如数组 这样我就知道输出是什么 假设我有一个数组 就像这样 File mp3List Log v test mp3List 为什么我不能将数组记录到控制台 我该怎么做 您无法记录该数组 因为它只是一个
  • unix/linux 套接字中的阻塞模式如何工作?

    阻塞模式是否将该特定任务置于 进程等待 状态 因为我认为非阻塞套接字需要用户明确的 忙等待 或 自旋锁 实现 或者阻塞模式套接字只不过是内核忙等待的隐式实现 在信号量 互斥体 监视器等锁定机制中 通常通过将任务推入阻塞状态来实现锁定 我认为
  • Zapier:修改 webhook 侦听器 HTTP 响应?

    某些 API 需要 Webhook 侦听器响应中的附加信息 例如 我正在尝试订阅 Outlook com 的推送通知服务 该服务描述了以下流程 Outlook 通知服务尝试使用侦听器服务验证通知 URL 它在验证请求中包含验证令牌 如果侦听
  • Google REcaptcha 未显示

    我的中有以下内容 div class g recaptcha 这在我的 但无论是在 Firefox 还是 Chrome 上都没有显示任何内容 这是一个已知问题吗 确保是 head 标签关闭之前的最后一件事 这为我解决了同样的问题 div
  • 工具提示问题,MatTooltip 在 Angular 中不起作用

    我正在尝试在仪表板页面中插入通知工具提示 但该工具提示不起作用 我对 Angular 非常陌生 因此任何有关此问题的线索都将受到高度赞赏 module ts import MatTooltipModule from angular mate
  • find 命令仅搜索非隐藏目录

    在以下命令中 我只想搜索非隐藏的目录 如何使用以下命令执行此操作 我想在搜索日志文件时忽略隐藏目录 find home tom project name log txt ls home tom project dir1 dir2 backu
  • JAXB 绑定嵌套元素

    我正在使用 JAXB impl 我需要能够将嵌套元素作为简单类型映射到类字段 例如
  • 在 JavaScript 中使用 for..of 迭代时从数组中删除元素应该是安全的吗?

    我知道它适用于Set 但我的印象是它也可以与 Array 一起使用 所以我在 Chrome 中尝试了一下 很惊讶它不起作用 const array 1 2 3 4 5 6 for const item of array if item 3
  • 如何获取文件的文件类型

    有没有办法让VB net中的Windows资源管理器中显示的文件类型 例如 在 Windows 资源管理器的详细信息视图中可以看到 Name Date Modified Type Size A PDF 05 06 2017 5 54PM A
  • 无法访问生成配置管理器或 Visual C# 2010 Express 中的生成配置

    完整故事 通常 当我安装 Visual C 2010 Express 时 我做的第一件事就是切换到专家设置 这使我可以访问构建配置以及相应的管理器 最近的安装似乎行为不当 我创建的第一个项目是 XNA 4 0 刷新 项目 我导入了一些旧代码
  • Cordova / Ionic - 从 InAppBrowser 下载文件

    场景是这样的 我在InAppBrowser中打开一个网站 用户结束那里的工作后 网站生成一个 pdf供用户下载 问题是pdf没有下载 它在浏览器 有没有办法让它从 InAppBrowser 下载 我目前正在开发一个 iOS 应用程序 因此该
  • Laravel APP_LOCALE 西班牙语

    在 Laravel 5 4 中 env I have APP LOCALE es APP FALLBACK LOCALE en APP LOCALE PHP es US and in config app php locale gt env
  • 如何将数据库路由器添加到 Django 项目

    我正在按照此处有关如何在一个 Django 项目中处理多个数据库的说明进行操作主题 数据库 多数据库 https docs djangoproject com en 2 1 topics db multi db 我已经创建了所需的两个路由器