在python库中使用Django ORM会导致麻烦吗? [英] Will using the Django ORM in a python library cause trouble?

查看:150
本文介绍了在python库中使用Django ORM会导致麻烦吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,我很喜欢使用Django。现在我正在为一家正在建立一些共享的内部库来访问我们的数据库中的信息的公司工作。现在,事情是非常糟糕的 - 大量的内联SQL代码。有些同事正在开发一些访问代码,在我看来,这是Django开箱即用的事情。我也听说Django基本上是模块化的,所以我只能孤立地使用ORM系统。我知道还有其他的软件包就像SqlAlchemy一样,但是我也听说Django对平均情况更容易,这可能是我们需要的。我也知道Django,不知道SQLAlchemy。



所以,我可以做一个概念证明,如下所示。这是目录结构:

  +  -  example.py 
+ - transfer
| + - __init__.py
| + - models.py

这里是models.py

  import django.conf 
import django.db.models

django.conf.settings.configure(
DATABASES = ...,#数据库信息
INSTALLED_APPS =(transfer,),
SECRET_KEY ='not tell',

django.setup()

class TransferItem(django.db.models.Model)
#model info here

example.py

  from transfer.models import TransferItem 
items = TransferItem.objects.all()
打印项目

尽管如此,这似乎工作正常但我担心图书馆上下文中的引导代码。具体来说:


  1. django有没有把这个视为一个应用程序的危险?在这种情况下,转移是一个根本的模块,但在图书馆的情况下,这可能是深埋的。例如mycompany.data.protocols.transfer。理论上,我们可以在整个代码库中定义这些数据模型。这个应用程序列表如何缩放?

  2. 对设置的调用真的让我很担心。 django文档专门说只有调用setup一次。而图书馆的本质就是任何python应用程序都可以导入任何类型的数据模型。我不能对他们想要的哪个django应用程序做任何假设,或者他们想要的是什么顺序。如果使用一种类型的模型,则返回数据,然后只有python应用程序决定需要导入另一种类型的模型(很可能在不同的应用程序)?

所以,根本的问题是这样的:



在python库中使用django ORM有好的方法吗?



编辑:这是 不是 重复的CLI工具问题。我知道如何在web服务器之外运行django。我甚至给出了代码示例。我想知道有没有办法运行django,我没有应用程序本身,其中模型文件可以由客户端代码导入,并以任何顺序使用。

解决方案

好的,这是我自己试图在一些研究后回答这个问题。



我可以为我的模型创建一个baseclass图书馆的任何地方如下:

 从django.db导入模型
从django.apps导入应用程序
import django.conf

django.conf_settings.configure(
DATABASES = ...


apps.populate((__ name__,))

class LibModel(models.Model):
class Meta:
abstract = True
app_label = __name__
pre>

然后在库中的任何地方,我可以使用这个基础类创建自己的模型。由于我不依赖数据库名称的app,我需要明确说明。

  class SpecificModel LibModel):
#fields去这里
class Meta(LibModel.Meta):
db_table =specific_model_table_name

这让我担心必须模拟应用程序的结构。基础类中的名称属性为Django提供了所有需要,然后Django退出关于没有找到应用程序。其他模型文件可以随时随地生活。



然而,仍然有一个很大的问题,我可能是致命的。 Django的初始化本身就是单例。如果我的图书馆本身是由Django应用程序导入的,那么它就会崩溃和刻录。我已经在这个后续问题


I've enjoyed using Django quite a bit over the years. Right now I'm working for a company that is building some shared internal libraries for accessing information from our databases. Right now things are terribly sloppy - lots of inline SQL code. Some colleagues were working on doing some accessing code, and it occurred to me that this is the sort of thing that I'm used to Django doing out of the box. I had also heard that Django is fundamentally modular, so that I could just use the ORM system in isolation. I know there are other packages for this like SqlAlchemy, but I also hear that Django does things easier for the average case, which is likely all we'll need. I also know Django, and don't know SQLAlchemy.

So, I can do a proof of concept, like the following. Here is the directory structure:

+-- example.py
+-- transfer
|   +-- __init__.py
|   +-- models.py

Here is models.py

import django.conf
import django.db.models

django.conf.settings.configure(
    DATABASES = ..., # database info
    INSTALLED_APPS = ("transfer",),
    SECRET_KEY = 'not telling',
)
django.setup()

class TransferItem(django.db.models.Model)
    # model info here

example.py

from transfer.models import TransferItem
items = TransferItem.objects.all()
print items

This seems to work fine, as far as it goes. But I'm worried about the bootstrap code in a library context. Specifically:

  1. Is there danger in django thinking of this as an app? In this case, "transfer" is a root module, but in a library context this could be buried deep. For example, "mycompany.data.protocols.transfer". Theoretically, we could have these data models defined throughout the codebase. How can this "app list" scale?
  2. The call to setup really worries me. The django docs specifically say only to call setup once. And yet the nature of a library is that any python application could import whatever type of data model they want. I can't make any assumptions about which django "apps" they might want, or what order they want it in. Would if one type of model is used, data is returned, and only then does the python application decide it needs to import another type of model (quite possibly in a different "app")?

So, the fundamental question is this:

Is there a good way to use django ORM in a python library?

EDIT: This is not a duplicate of the CLI tool question. I know how to run django outside the web server. I even gave code samples showing this. I want to know if there's a way to run django where I don't have "apps" per se - where model files could be imported by client code and used in any order.

解决方案

OK, here's my own attempt to answer the question after some research.

I can create a baseclass for my models anywhere in the library as follows:

from django.db import models
from django.apps import apps
import django.conf

django.conf_settings.configure(
    DATABASES = ...
)

apps.populate((__name__,))

class LibModel(models.Model):
    class Meta:
        abstract = True
        app_label = __name__

Then anywhere in the library I can create my own models with this baseclass. Since I'm not relying on the "app" for the database names, I need to state them explicitly.

class SpecificModel(LibModel):
    # fields go here
    class Meta(LibModel.Meta):
        db_table = "specific_model_table_name"

This gets around my concern of having to simulate the structure of an "app". The name property in the base class supplies Django with all it needs, and then Django quits whining about not finding an app. The other model files can live wherever they want.

However, there is still a big concern I have which might be lethal. Django's initialization itself is still singleton in nature. If my library were itself imported by a Django application, it would all crash and burn. I've asked for solutions to this in this follow up question.

这篇关于在python库中使用Django ORM会导致麻烦吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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