python - Django 中不同数据库中存储的模型之间可以建立关系吗?

标签 python django database security web

我目前正在开发一个项目,在该项目中我处理公共(public)信息和私有(private)信息,这些信息都以不同的模型存储在公共(public)数据库中。

我想将此数据库分成两部分,一个包含私有(private)模型对象,另一个包含公共(public)模型对象。

问题是,这两个模型彼此之间都有外键关系,并且我在互联网上发现了相互矛盾的答案,即即使模型位于两个不同的数据库中,这种关系是否也有效。

那么,这可能吗?有更好的方法吗?

只是为了澄清我为什么要这样做,我希望该项目是开源的,因此公共(public)数据库应该是公开的,但敏感信息(用户和密码)应该保密。

最佳答案

来自 Django docs :

Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.

This is because of referential integrity. In order to maintain a relationship between two objects, Django needs to know that the primary key of the related object is valid. If the primary key is stored on a separate database, it’s not possible to easily evaluate the validity of a primary key.

有关可能的解决方案,请查看此讨论:https://stackoverflow.com/a/32078727/14209813

关于python - Django 中不同数据库中存储的模型之间可以建立关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69861463/

相关文章:

javascript - Jquery tablesorter 插件在 Django 模板中不起作用

mysql - 如何优化这个数据表?

mysql - 使用 R 从 phpmyadmin 连接到本地数据库

mysql - 在 mysql 中替换时保留一列

python - RethinkDB,使用 map 和过滤器进行查询

python - 为什么 a//b 和 np.floor(a/b) 产生不同的结果?

python - 提高 headless Selenium 的性能

python - Sympy 表达式为多项式之比

django - Django 表单下拉列表中的初始值不为空?

django - 使用自定义身份验证时如何为 Firebase 编写安全规则?