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

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

问题描述

可能的重复:
仅使用 Django 的某些部分?
仅使用 Django 的数据库部分

我想独立使用 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:

  • 是否需要我使用 setting.py、/myApp/目录和 modules.py 文件来设置我的 Python 项目?
  • 我可以创建一个新的 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 inspectdb ...

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 项目的 manage.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:

djangoconfproject_templatemanage.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:

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