8

I have two apps that both access the same database. The first has clients connecting via TCP and writes to the db using SQLAlchemy. The second is a more typical webapp using Django. Both have read/write requirements.

I would like to unify the database access layer, but picking just SQLAlchemy or just Django is unattractive because:

  1. I would like to use django auth, permissions, and maybe third party plugins, which require the Django ORM (correct me if I'm wrong).
  2. For the first app, using SQLAlchemy (so far) is much simpler than trying to use the Django ORM outside of a Django app - it is a TCP/IP server app, not a HTTP/web app.

Are there any problems with mixing these two ORMs on the same database?

In which system (Django, SQLA) should I create the models, vs using some kind of introspection such as Django inspectdb?

4

1 回答 1

7

manage.py首先 - 在、WSGI 处理程序和其他与 HTTP 相关的东西之外使用 Django ORM 并不难。您可以在任何 python 脚本中使用它,但它需要一些初始化(示例)。

其次 - SQLA 是一个非常强大的工具,它能够完成在 Django ORM 中很难实现的事情(如真正的多态性和多态性查询)。如果必须选择,我个人会选择使用 Django ORM 作为创建模型的平台,然后在 SQLA 中手动映射它们,因为它更灵活,希望能够采用。在相反的情况下可能不起作用。

最后,由于你可以在两边都使用 Django ORM,而你只需要使用 Django ORM 因为插件,我建议放弃 SQLA。这是一个强大的工具,但也相当复杂。在一个数据库上运行两个不同的 ORM 可能会导致未来出现意外问题并增加应用程序的复杂性,因此维护起来会更加困难。

于 2012-06-01T22:52:12.880 回答