使用Django ORM独立 [英] Use Django ORM as standalone

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

问题描述


可能的重复:

仅使用Django的某些部分?

仅使用Django的DB部分

我想将Django ORM用作独立。尽管有一小时搜索Google,但我还是留下了几个问题:

I want to use the Django ORM as standalone. Despite an hour of searching Google, I'm still left with several questions:


  • 需要我设置我的Python项目setting.py,/ myApp /目录和modules.py文件?

  • 我可以创建一个新的 models.py 并运行 syncdb 可以自动设置表和关系,还是只能使用现有Django项目的模型?

  • 似乎有很多关于 PYTHONPATH 的问题。如果你不是在调用现有的模型,这是否需要?

  • Does it require me to set up my Python project with a setting.py, /myApp/ directory, and modules.py file?
  • Can I create a new models.py and run syncdb to have it automatically setup the tables and relationships or can I only use models from existing Django projects?
  • There seems to be a lot of questions regarding PYTHONPATH. If you're not calling existing models is this needed?

我想最简单的事情是让一个人发布一个基本的模板或演练过程,澄清文件的组织,例如:

I guess the easiest thing would be for someone to just post a basic template or walkthrough of the process, clarifying the organization of the files e.g.:

db/
   __init__.py
   settings.py
   myScript.py
orm/
   __init__.py
   models.py

基本要点:

# settings.py
from django.conf import settings

settings.configure(
     DATABASE_ENGINE   = "postgresql_psycopg2",
     DATABASE_HOST     = "localhost",
     DATABASE_NAME     = "dbName",
     DATABASE_USER     = "user",
     DATABASE_PASSWORD = "pass",
     DATABASE_PORT     = "5432"
)

# orm/models.py
# ...

# myScript.py
# import models..

而且您是否需要运行以下内容: django-admin.py inspectd b ...

And whether you need to run something like: django-admin.py inspectdb ...

(哦,如果这些更改关于命令行参数的话,我正在运行Windows)。

(Oh, I'm running Windows if that changes anything regarding command-line arguments.).

推荐答案

啊,我想出来,并会为任何试图做同样事情的人发布解决方案。

Ah ok I figured it out and will post the solutions for anyone attempting to do the same thing.

此解决方案假定您要创建新模型。

This solution assumes that you want to create new models.

首先创建一个新文件夹来存储文件。我们称之为standAlone。在standAlone中创建以下文件:

First create a new folder to store your files. We'll call it "standAlone". Within "standAlone", create the following files:

__init__.py
myScript.py
settings.py

显然,myScript.py可以命名。

Obviously "myScript.py" can be named whatever.

接下来,为您的模型创建一个目录。

Next, create a directory for your models.

我们将命名我们的模型目录myApp但是意识到这是项目中的一个正常的Django应用程序,因此,它适合于您正在编写的模型集合。

We'll name our model directory "myApp", but realize that this is a normal Django application within a project, as such, name it appropriately to the collection of models you are writing.

在此目录下创建2个文件:

Within this directory create 2 files:

__init__.py
models.py

您需要一个现有的你可以从Django安装路径中获取一个副本:

Your going to need a copy of manage.py from an either an existing Django project or you can just grab a copy from your Django install path:

django\conf\project_template\manage.py

将manage.py复制到/ standAlone目录。好的,所以你现在应该具有以下结构:

Copy the manage.py to your /standAlone directory. Ok so you should now have the following structure:

\standAlone
    __init__.py
    myScript.py
    manage.py
    settings.py
\myApp
    __init__.py
    models.py

将以下内容添加到myScript.py文件中:

# settings.py
from django.conf import settings

settings.configure(
    DATABASE_ENGINE    = "postgresql_psycopg2",
    DATABASE_NAME      = "myDatabase",
    DATABASE_USER      = "myUsername",
    DATABASE_PASSWORD  = "myPassword",
    DATABASE_HOST      = "localhost",
    DATABASE_PORT      = "5432",
    INSTALLED_APPS     = ("myApp")
)

from django.db import models
from myApp.models import *

并将其添加到您的settings.py文件中:

    DATABASE_ENGINE    = "postgresql_psycopg2"
    DATABASE_NAME      = "myDatabase"
    DATABASE_USER      = "myUsername"
    DATABASE_PASSWORD  = "myPassword"
    DATABASE_HOST      = "localhost"
    DATABASE_PORT      = "5432",
    INSTALLED_APPS     = ("myApp")

最后你的myApp / models.py: p>

and finally your myApp/models.py:

# myApp/models.py
from django.db import models

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

。现在让Django管理你的数据库,在命令提示符下导航到我们的/ standalone目录并运行:

and that's it. Now to have Django manage your database, in command prompt navigate to our /standalone directory and run:

manage.py sql MyApp

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

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