如何在Django之外使用Django模型? [英] How to use Django models outside of Django?

查看:146
本文介绍了如何在Django之外使用Django模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下指南,通过调用 python main.py ,我可以在Django外部使用具有以下文件结构的模型。

Following this guide, I am able to use models outside of Django with the following file structure by calling python main.py.

├── data
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   └── models.py
├── main.py
├── manage.py
└── settings.py

其中main.py看起来像这样:

where main.py looks like this:

import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

django.setup()
from data.models import Foo, Bar #...

print(Foo.objects.all()) #this works fine

我想要做的是将其转换为一个名为 db 的程序包,该程序包如下所示:

What I want to do is turn this into a "package" called db that looks like this:

    ├── data
    │   ├── __init__.py
    │   ├── migrations
    │   │   └── __init__.py
    │   └── models.py
    ├── __init__.py 
    ├── manage.py
    └── settings.py

__ init __。py 中db 软件包,我想这样做:

And in the __init__.py of the db package, I want to do this:

import os, django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

django.setup()
from data.models import Foo, Bar # ...
from django.db import connection

__all__ = [
    'connection',
    'Foo',
    'Bar', 
    #...
]

所以我可以从< db 包中调用 test.py (与 db 在同一目录中),如下所示:

So I can call the db package from test.py (which is in the same directory as db) like this:

import db

print(db.Foo.objects.all()) #this throws an error "no module named data"

或类似这样:

from db import Foo

print(Foo.objects.all()) # this throws an error "no module named settings"

有没有一种方法可以使用Django的模型而无需调用:
os.environ.setdefault('DJANGO_SETTINGS_MODULE','设置')
django.setup()

Is there a way I can use Django's models without having to call: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') django.setup()

我在这里缺少什么?

推荐答案

如果您查看已加载Django应用我认为您需要在应用的 models.py 中运行设置models / init.py 而不是 db / __ init __。py

If you check out how Django apps are loaded I think you need to run your setup in your application’s models.py or models/init.py and not in db/__init__.py


Django启动时,django.setup()负责填充
应用程序注册表。

setup( set_prefix = True)通过以下方式配置Django:

setup(set_prefix=True) Configures Django by:

加载设置。设置日志记录。如果set_prefix为True,则
如果已定义
,则将URL解析器脚本前缀设置为FORCE_SCRIPT_NAME,否则/否则。初始化应用程序注册表。
函数会自动调用:

Loading the settings. Setting up logging. If set_prefix is True, setting the URL resolver script prefix to FORCE_SCRIPT_NAME if defined, or / otherwise. Initializing the application registry. This function is called automatically:

通过Django的WSGI支持运行HTTP服务器时。调用
管理命令时。在其他情况下,必须使用纯Python脚本中的
实例显式调用它。

When running an HTTP server via Django’s WSGI support. When invoking a management command. It must be called explicitly in other cases, for instance in plain Python scripts.

应用程序注册表分为三个阶段初始化。在每个
阶段,Django都会按
INSTALLED_APPS的顺序处理所有应用程序。

The application registry is initialized in three stages. At each stage, Django processes all applications in the order of INSTALLED_APPS.

第一个Django会在INSTALLED_APPS中导入每个项目。

First Django imports each item in INSTALLED_APPS.

如果是应用程序配置类,则Django会导入应用程序的根
包,该包由其name属性定义。如果它是
Python软件包,则Django将创建默认的应用程序配置。

If it’s an application configuration class, Django imports the root package of the application, defined by its name attribute. If it’s a Python package, Django creates a default application configuration.

在此阶段,您的代码不应导入任何模型!

At this stage, your code shouldn’t import any models!

换句话说,不应导入应用程序的根包和
定义应用程序配置类的模块

In other words, your applications’ root packages and the modules that define your application configuration classes shouldn’t import any models, even indirectly.

严格来说,Django允许在加载
应用程序配置后导入模型。
但是,为了避免
对INSTALLED_APPS的顺序产生不必要的约束,强烈建议
在此阶段不要导入任何模型。

Strictly speaking, Django allows importing models once their application configuration is loaded. However, in order to avoid needless constraints on the order of INSTALLED_APPS, it’s strongly recommended not import any models at this stage.

阶段完成后,在应用程序
配置(例如get_app_config())上运行的API变得可用。

Once this stage completes, APIs that operate on application configurations such as get_app_config() become usable.

然后Django尝试导入每个
的models子模块

Then Django attempts to import the models submodule of each application, if there is one.

您必须定义或即时将所有模型移植到应用程序的models.py
或models / __ init__.py中。否则,此时应用程序注册表可能没有完全填充
,这可能导致ORM出现
故障。

完成此阶段后,可以在
get_model()等模型上运行的API可用。

Once this stage completes, APIs that operate on models such as get_model() become usable.

最后Django在每个应用程序$ b $上运行ready()方法b配置。

Finally Django runs the ready() method of each application configuration.

这篇关于如何在Django之外使用Django模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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