python SQLAlchemy 和 django,生产准备好了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1154331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLAlchemy and django, is it production ready?
提问by Piotr Czapla
Has anyone used SQLAlchemy
in addition to Django
's ORM?
有没有人用过SQLAlchemy
除了Django
's ORM?
I'd like to use Django's ORM for object manipulation and SQLalchemy for complex queries (like those that require left outer joins).
我想使用 Django 的 ORM 进行对象操作,使用 SQLalchemy 进行复杂查询(例如需要左外连接的查询)。
Is it possible?
是否可以?
Note: I'm aware about django-sqlalchemy
but the project doesn't seem to be production ready.
注意:我知道django-sqlalchemy
但该项目似乎尚未准备好生产。
采纳答案by agiliq
What I would do,
我会怎么做,
Define the schema in Django orm, let it write the db via syncdb. You get the admin interface.
In view1 you need a complex join
在 Django orm 中定义架构,让它通过同步数据库写入数据库。您将获得管理界面。
在 view1 中你需要一个复杂的连接
def view1(request):
import sqlalchemy
data = sqlalchemy.complex_join_magic(...)
...
payload = {'data': data, ...}
return render_to_response('template', payload, ...)
回答by Vinay Sajip
I don't think it's good practice to use both. You should either:
我不认为同时使用两者是一个好习惯。你应该:
- Use Django's ORM and use custom SQL where Django's built-in SQL generation doesn't meet your needs, or
- Use SQLAlchemy (which gives you finer control at the price of added complexity) and, if needed, use a declarative layer such as Elixirto make life a little easier.
- 使用 Django 的 ORM 并使用自定义 SQL,其中 Django 的内置 SQL 生成不能满足您的需求,或者
- 使用 SQLAlchemy(它以增加复杂性为代价为您提供更好的控制),并且如果需要,使用诸如Elixir 之类的声明性层使生活更轻松。
Of course, if you need Django's admin, then the first of these approaches is recommended.
当然,如果您需要 Django 的管理员,那么推荐使用这些方法中的第一种。
回答by Stuart Axon
I've done it before and it's fine. Use the SQLAlchemy feature where it can read in the schema so you don't need to declare your fields twice.
我以前做过,没问题。使用 SQLAlchemy 功能,它可以在架构中读取,这样您就不需要两次声明您的字段。
You can grab the connection settings from the settings, the only problem is stuff like the different flavours of postgres driver (e.g. with psyco and without).
您可以从设置中获取连接设置,唯一的问题是诸如不同风格的 postgres 驱动程序(例如使用 psyco 和不使用 psyco)之类的东西。
It's worth it as the SQLAlchemy stuff is just so much nicer for stuff like joins.
这是值得的,因为 SQLAlchemy 的东西对于连接之类的东西来说要好得多。
回答by Stuart Axon
Jacob Kaplan-Moss admitted to typing "import sqlalchemy" from time to time. I may write a queryset adapter for sqlalchemy results in the not too distant future.
Jacob Kaplan-Moss 承认不时键入“import sqlalchemy”。在不久的将来,我可能会为 sqlalchemy 结果编写一个查询集适配器。