仅使用 Django 的 DB 部分 [英] Using only the DB part of Django

查看:20
本文介绍了仅使用 Django 的 DB 部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道 Django 有多模块化"吗?我可以只使用 ORM 部分来获取映射到数据库表的类并知道如何从这些表中读取/写入吗?

Does somebody know how "modular" is Django? Can I use just the ORM part, to get classes that map to DB tables and know how to read/write from these tables?

如果不是,你会推荐什么作为Hibernate 的 Python 等价物"?

If not, what would you recommend as "the Python equivalent of Hibernate"?

推荐答案

简短的回答是:不,您不能将 Django ORM 与 Django 分开使用.

The short answer is: no, you can't use the Django ORM separately from Django.

答案很长:是的,如果您愿意同时加载 Django 的大部分内容,就可以.例如,当向 Django 发出请求时,就会打开 Django 使用的数据库连接.当发送信号时会发生这种情况,因此您可以在表面上发送此信号以打开连接,而无需使用特定的请求机制.此外,您还需要为 Django 项目设置各种应用程序和设置.

The long answer is: yes, you can if you are willing to load large parts of Django along with it. For example, the database connection that is used by Django is opened when a request to Django occurs. This happens when a signal is sent so you could ostensibly send this signal to open the connection without using the specific request mechanism. Also, you'd need to setup the various applications and settings for the Django project.

最终,这可能不值得您花时间.SQL Alchemy 是一个相对知名的 Python ORM,实际上它比 Django 的更强大,因为它支持多个数据库连接以及连接池和其他好东西.

Ultimately, it probably isn't worth your time. SQL Alchemy is a relatively well known Python ORM, which is actually more powerful than Django's anyway since it supports multiple database connections and connection pooling and other good stuff.

为了回应詹姆斯在别处的批评,我将澄清我在原帖中描述的内容.虽然令人欣慰的是一位主要的 Django 贡献者把我叫出来,但我仍然认为我是对的 :)

in response to James' criticism elsewhere, I will clarify what I described in my original post. While it is gratifying that a major Django contributor has called me out, I still think I'm right :)

首先,考虑需要做什么才能将 Django 的 ORM 与任何其他部分分开使用.您使用的方法之一James 对 Django 进行了基本设置.但是其中许多方法不允许使用 syncdb 命令,这是为您的模型创建表所必需的.为此需要一个 settings.py 文件,其中不仅包含 DATABASE_* 的变量,还包含 INSTALLED_APPLICATIONS 的变量以及所有 models.py 文件的正确路径.

First off, consider what needs to be done to use Django's ORM separate from any other part. You use one of the methods described by James for doing a basic setup of Django. But a number of these methods don't allow for using the syncdb command, which is required to create the tables for your models. A settings.py file is needed for this, with variables not just for DATABASE_*, but also INSTALLED_APPLICATIONS with the correct paths to all models.py files.

可以推出自己的解决方案以使用 syncdb 而无需 settings.py,但这需要一些 Django 的高级知识.当然,你不需要使用syncdb;表格可以独立于模型创建.但这是 ORM 的一个方面,除非您付出一些努力进行设置.

It is possible to roll your own solution to use syncdb without a settings.py, but it requires some advanced knowledge of Django. Of course, you don't need to use syncdb; the tables can be created independently of the models. But it is an aspect of the ORM that is not available unless you put some effort into setup.

其次,考虑如何使用标准的 Model.objects.filter() 调用创建对数据库的查询.如果这是作为视图的一部分完成的,则非常简单:构造 QuerySet 并查看实例.例如:

Secondly, consider how you would create your queries to the DB with the standard Model.objects.filter() call. If this is done as part of a view, it's very simple: construct the QuerySet and view the instances. For example:

tag_query = Tag.objects.filter( name='stackoverflow' )
if( tag_query.count() > 0 ):
    tag = tag_query[0]
    tag.name = 'stackoverflowed'
    tag.save()

漂亮、简单、干净.现在,没有 Django 的请求/响应链系统的支持,您需要初始化数据库连接,进行查询,然后关闭连接.所以上面的例子变成了:

Nice, simple and clean. Now, without the crutch of Django's request/response chaining system, you need to initialise the database connection, make the query, then close the connection. So the above example becomes:

from django.db import reset_queries, close_connection, _rollback_on_exception
reset_queries()
try:
    tag_query = Tag.objects.filter( name='stackoverflow' )
    if( tag_query.count() > 0 ):
        tag = tag_query[0]
        tag.name = 'stackoverflowed'
        tag.save()
except:
    _rollback_on_exception()
finally:
    close_connection()

数据库连接管理也可以通过 Django 信号来完成.以上所有内容都在 django/db/init 中定义.py.其他 ORM 也有这种连接管理,但您无需深入研究它们的来源以了解如何执行此操作.SQL Alchemy 的连接管理系统记录在教程和其他地方.

The database connection management can also be done via Django signals. All of the above is defined in django/db/init.py. Other ORMs also have this sort of connection management, but you don't need to dig into their source to find out how to do it. SQL Alchemy's connection management system is documented in the tutorials and elsewhere.

最后,您需要记住,数据库连接对象始终是当前线程的本地对象,这可能会或可能不会限制您,具体取决于您的要求.如果您的应用程序不是无状态的,如 Django,而是持久的,您可能会遇到线程问题.

Finally, you need to keep in mind that the database connection object is local to the current thread at all times, which may or may not limit you depending on your requirements. If your application is not stateless, like Django, but persistent, you may hit threading issues.

总之,这是一个见仁见智的问题.在我看来,Django 与框架分离的 ORM 的局限性和所需的设置都是一种负担.其他地方有完全可行的专用 ORM 解决方案,专为库使用而设计.姜戈不是.

In conclusion, it is a matter of opinion. In my opinion, both the limitations of, and the setup required for, Django's ORM separate from the framework is too much of a liability. There are perfectly viable dedicated ORM solutions available elsewhere that are designed for library usage. Django's is not.

不要认为以上所有内容都表明我不喜欢 Django 及其所有工作,我真的很喜欢 Django!但我对它的功能很现实,作为 ORM 库不是其中之一.

Don't think that all of the above shows I dislike Django and all it's workings, I really do like Django a lot! But I'm realistic about what it's capabilities are and being an ORM library is not one of them.

附言多数据库连接支持正在工作.但现在没有了.

P.S. Multiple database connection support is being worked on. But it's not there now.

这篇关于仅使用 Django 的 DB 部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆