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

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

问题描述

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



如果没有,你会推荐什么Python的等效于Hibernate?

解决方案

简短的答案是:不,你不能单独使用Django ORM Django。



长的答案是:是的,如果你愿意加载Django的大部分,就可以了。例如,当Django发出请求时,Django使用的数据库连接将被打开。当发送信号时,会发生这种情况,以便您可以表面上发送此信号来打开连接,而不使用特定的请求机制。此外,您需要为Django项目设置各种应用程序和设置。



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






编辑:我会澄清我在原文中描述的内容。尽管一个主要的Django贡献者打电话给我很高兴,但我仍然认为我是对的:)



首先,考虑使用Django的ORM与任何其他部分分开。您使用以下描述的方法之一詹姆斯做了Django的基本设置。但是,许多这些方法不允许使用为您的模型创建表所需的 syncdb 命令。需要一个settings.py文件,变量不仅仅适用于 DATABASE _ * ,而且 INSTALLED_APPLICATIONS 具有正确的路径到所有models.py文件。



可以滚动自己的解决方案,使用 syncdb 而不设置.py,但它需要一些Django的高级知识。当然,你不需要使用 syncdb ;可以独立于模型创建表。但是,ORM的一个方面是无法使用的,除非您在设置中付出了一些努力。



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

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

尼斯,简单而干净。现在,没有Django的请求/响应链系统的拐点,您需要初始化数据库连接,进行查询,然后关闭连接。所以上面的例子变成:

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

数据库连接管理也可以通过Django信号完成。以上所有内容都在 django / db / init中定义的.py 。其他ORM也有这种连接管理,但是您不需要挖掘他们的来源来了解如何做到这一点。 SQL Alchemy的连接管理系统记录在教程及其他地方。



最后,您需要记住,数据库连接对象始终是当前线程本地的,根据您的要求,这可能会限制您的使用或不限制。如果您的应用程序不是无状态的,就像Django一样,但是持续存在,您可能会遇到线程问题。



总之,这是一个意见问题。在我看来,Django的ORM与框架分开的限制和设置都是太大的责任。在其他地方提供了专门用于图书馆使用的完全可行的专用ORM解决方案。 Django不是



不要以为所有上述都显示我不喜欢Django和所有它的工作,我真的很喜欢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?

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

解决方案

The short answer is: no, you can't use the Django ORM separately from 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.

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.


Edit: 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 :)

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.

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.

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()

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()

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.

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.

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.

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天全站免登陆