python 只使用 Django 的某些部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302651/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:51:36  来源:igfitidea点击:

Use only some parts of Django?

pythondjango

提问by Hanno Fietz

I like Django, but for a particular application I would like to use only parts of it, but I'm not familiar enough with how Django works on the inside, so maybe someone can point me into the right direction as to what I have to check out.

我喜欢 Django,但对于一个特定的应用程序,我只想使用它的一部分,但我对 Django 在内部的工作方式还不够熟悉,所以也许有人可以指出我必须做的正确方向查看。

Specifically, I want to use:

具体来说,我想使用

  • The models and database abstraction
  • The caching API, although I want to avoid database lookups by caching, not HTML generation, and since the caching framework in Django is intended for the latter, I'm not sure yet whether that's really appropriate.
  • 模型和数据库抽象
  • 缓存API,但我想通过缓存来避免数据库查询,而不是HTML生成,并且由于在Django的缓存框架适用于后者,我不知道是否还没有这真的合适。

I would not use:

不会使用

  • Templating
  • urlconfigs
  • 模板制作
  • urlconfigs

Or, more exactly, I'm neither using HTTP nor HTML. So basically, I have a different input / output chain than usual.

或者,更准确地说,我既不使用 HTTP 也不使用 HTML。所以基本上,我有一个与平时不同的输入/输出链。

Can this work?

这能行吗?

My personal killer feature in Django is the Object / database mapping that I can do with the models, so if there's another technology (doesn't have to be Python, I'm in the design phase and I'm pretty agnostic about languages and platforms) that gives me the same abilities, that would be great, too.

我在 Django 中的个人杀手级功能是我可以用模型做的对象/数据库映射,所以如果有另一种技术(不一定是 Python,我正处于设计阶段,我对语言和平台)给了我同样的能力,那也很棒。

回答by Eli Courtwright

I myself use Django for its object/db mapping without using its urlconfigs. Simply create a file called djangosettings.pyand insert the necessary configuration, for example:

我自己使用 Django 进行对象/数据库映射,而不使用它的 urlconfigs。只需创建一个名为的文件djangosettings.py并插入必要的配置,例如:

DATABASE_ENGINE   = 'oracle'
DATABASE_HOST     = 'localhost'
DATABASE_NAME     = 'ORCL'
DATABASE_USER     = 'scott' 
DATABASE_PASSWORD = 'tiger'

Then in your regular Python code, do

然后在您的常规 Python 代码中,执行

import os
os.environ["DJANGO_SETTINGS_MODULE"] = "djangosettings"

before you import any Django modules. This will let you use Django's object/db mappings without actually having a Django project, so you can use it for standalone scripts or other web applications or whatever you want.

在导入任何 Django 模块之前。这将使您无需实际拥有 Django 项目即可使用 Django 的对象/数据库映射,因此您可以将其用于独立脚本或其他 Web 应用程序或任何您想要的。

As for caching, if you don't want to use Django then you should probably decide what you are using and go from there. I recommend using CherryPy, which doesn't use Django-style regular expression URL mapping, but instead automatically maps URLs to functions based on the function names. There's an example right at the top of the CherryPy home page: http://cherrypy.org/

至于缓存,如果您不想使用 Django,那么您可能应该决定使用什么并从那里开始。我推荐使用 CherryPy,它不使用 Django 风格的正则表达式 URL 映射,而是根据函数名称自动将 URL 映射到函数。CherryPy 主页顶部有一个示例:http: //cherrypy.org/

CherryPy has its own caching system, so you can accomplish exactly the same thing as what Django does but without needing to use Django's urlconfig system.

CherryPy 有自己的缓存系统,因此您可以完成与 Django 完全相同的事情,而无需使用 Django 的 urlconfig 系统。

回答by user21799

Django, being a web framework, is extremely efficient at creating websites. However, it's also equally well-suited to tackling problems off the web. This is the loose couplingthat the project prides itself on. Nothing stops you from installing a complete version of Django, and just using what you need. As a rule, very few components of Django make broad assumptions about their usage.

Django 作为一个 Web 框架,在创建网站方面非常高效。然而,它也同样适用于解决网络问题。这是该项目引以为豪的松散耦合。没有什么能阻止您安装完整版本的 Django,并且只使用您需要的东西。通常,Django 中很少有组件对其使用做出广泛的假设。

Specifically:

具体来说:

One of the main things you'll face when trying to use Django without a web server is setting up the environment properly. The ORM and cache system still need to be configured in settings.py. There are docs on using django without a settings modulethat you may find useful.

在没有 Web 服务器的情况下尝试使用 Django 时,您将面临的主要问题之一是正确设置环境。ORM和缓存系统仍然需要在settings.py中进行配置。有关于在没有设置模块的情况下使用 django 的文档,您可能会发现它们很有用。

回答by devdrc

I've created a template Django project that allows you to do just that.

我创建了一个模板 Django 项目,允许您这样做。

https://github.com/dancaron/Django-ORM

https://github.com/dancaron/Django-ORM

Just follow the instructions and you can write standalone python files that utilize Django's database functionality, without having to use urlconf, views, etc.

只需按照说明操作,您就可以编写利用 Django 数据库功能的独立 python 文件,而无需使用 urlconf、视图等。

回答by devdrc

There are of course other projects out there that specifically implement single parts of django. TurboGearsfor example is a collection of several projects that can work by themselves and together form a complete web development framework.

当然还有其他项目专门实现了 django 的单个部分。例如,TurboGears是几个项目的集合,它们可以独立工作并共同形成一个完整的 Web 开发框架。

For the db abstraction SQLAlchemycomes to mind.

对于数据库抽象SQLAlchemy 浮现在脑海。

Regarding the caching part: I'm not aware of any standalone project that implements a generic caching facility.

关于缓存部分:我不知道任何实现通用缓存设施的独立项目。

On the other hand, it should be fairly easy to implement your own caching, for example by using pickles. Have a look at this recipe for a decoratorfor ideas and google for "memoize".

另一方面,实现自己的缓存应该相当容易,例如使用pickles。看看这个关于创意装饰者的食谱,并在谷歌上搜索“记忆”。

Also keep in mind that your database has its own caching mechanism, so maybe you don't even need to concern yourself with the details.

还要记住,您的数据库有自己的缓存机制,所以也许您甚至不需要关心细节。

回答by Jason Baker

I tend to prefer a mix-and-match approach to using Python for web programming. :-)

我倾向于使用混合搭配的方法来使用 Python 进行 Web 编程。 :-)

I don't have a lot of experience with Django, but I'd recommend giving sqlalchemya look for the database stuff. It works well with others and gives you several potential layers of abstraction (so you can go with something basic or tweak the hell out of it if you want). Plus, you'll already be somewhat familiar with it if you've ever used hibernate/nhibernate.

我对 Django 没有太多经验,但我建议让sqlalchemy看看数据库的东西。它适用于其他人,并为您提供了几个潜在的抽象层(因此您可以使用一些基本的东西或根据需要对其进行调整)。另外,如果您曾经使用过 hibernate/nhibernate,您就会对它有些熟悉。

My favorite part is that it has a lot of options for databases to connect to (most notably SQL Server, which django doesn't have built in last time I checked).

我最喜欢的部分是它有很多可供数据库连接的选项(最显着的是 SQL Server,我上次检查时 django 没有内置)。

With that said, I'm told that with Django, it's pretty easy to decouple functionality (but never done so myself).

话虽如此,有人告诉我,使用 Django,分离功能非常容易(但我自己从未这样做过)。

回答by IvanJijon

In order to use Django's models and database abstractionI explain a clean way to use them outside of Django here: https://stackoverflow.com/a/49515366/2682613

为了使用 Django 的模型和数据库抽象,我在这里解释了一种在 Django 之外使用它们的干净方法:https: //stackoverflow.com/a/49515366/2682613

Django version 2.0.2

Django 2.0.2 版

回答by Andrii Skaliuk

I've shared an example of solution, which prevents Python Path manipulation inside code:

我已经分享了一个解决方案示例,它可以防止代码中的 Python 路径操作:

https://github.com/askalyuk/django-orm-standalone

https://github.com/askalyuk/django-orm-standalone

It contains a standalone data access package, a separated simple Django site and a unit test.

它包含一个独立的数据访问包、一个单独的简单 Django 站点和一个单元测试。

回答by Gary Gauh

I found KeyboardInterrupt's answerbut it was answered in 2009 and I failed to run it in Django 1.8.For recent Django 1.8, You can have a look at this, in which some parts come from KeyboardInterrupt's answer.

我找到了KeyboardInterrupt 的答案,但它在 2009 年得到了回答,我未能在 Django 1.8 中运行它。最近Django 1.8,你可以看看这个,其中一些部分来自于 KeyboardInterrupt 的答案。

The folder structure is:

文件夹结构为:

.
├── myApp
│?? ├── __init__.py
│?? └── models.py
└── my_manage.py

myApp is a module, contains an empty __init__.pyand models.py.

myApp 是一个模块,包含一个空的__init__.pymodels.py.

There is an example model class in models.py: from django.db import models

有一个示例模型类models.py:from django.db import models

class MyModel(models.Model):
     field = models.CharField(max_length=255)

my_manage.py contains django database, installed_app settings and acts as django offical manage.py, so you can:

my_manage.py 包含 django 数据库、installed_app 设置并充当 django 官方 manage.py,因此您可以:

python my_manage.py sql myApp
python my_manage.py migrate
......

The codes in my_manage.pyare: from django.conf import settings

中的代码my_manage.py是: from django.conf 导入设置

db_conf = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_database_name',
        'USER': 'your_user_name',
        'PASSWORD': 'your_password',
        'HOST': 'your_mysql_server_host',
        'PORT': 'your_mysql_server_port',
    }
}

settings.configure(
    DATABASES = db_conf,
    INSTALLED_APPS     = ( "myApp", )
)

# Calling django.setup() is required for “standalone” Django u usage
# https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage
import django
django.setup()

if __name__ == '__main__':
    import sys
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)