如何在不创建Django项目的情况下使用Django 1.8.5 ORM? [英] How to use Django 1.8.5 ORM without creating a django project?

查看:113
本文介绍了如何在不创建Django项目的情况下使用Django 1.8.5 ORM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的一个Web应用程序中使用了Django ORM,对此我感到非常满意.现在,我有了一个新的需求,它需要数据库,而Django则没有其他要求.我不想花更多的时间来学习另一个像sqlalchemy的ORM.

I've used Django ORM for one of my web-app and I'm very much comfortable with it. Now I've a new requirement which needs database but nothing else that Django offers. I don't want to invest more time in learning one more ORM like sqlalchemy.

我想我仍然可以做
from django.db import models
并创建模型,但是如果没有manage.py,我将如何进行迁移和同步?

I think I can still do
from django.db import models
and create models, but then without manage.py how would I do migration and syncing?

推荐答案

对于旧版本的Django,有很多答案,但是Django一直在更新,在我的研究中,我发现Django 1.8没有可行的答案/1.9,所以我不得不自己动手.操作方法如下:

There are a lot of answers out there that work with older versions of Django, but Django is constantly updating and in my research I found no viable answer for Django 1.8/1.9, so I had to roll my own. Here's how you do it:

项目结构:

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

data目录和迁移目录包含空的__init__.py文件.样本models.py文件的内容如下:

The data directory and migrations directory contain empty __init__.py files. The sample models.py file reads as follows:

# models.py
from django.db import models

class User(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField(max_length=255)

manage.py文件是典型的Django manage.py文件.如果要从新的django-admin startproject命令复制它,只需确保更改os.environ.setdefault中的设置参数即可:

The manage.py file is the typical Django manage.py file. Just be sure to change the settings param in os.environ.setdefault if you copy it from a fresh django-admin startproject command:

#!/usr/bin/env python
# manage.py
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

settings.py文件需要3个设置:DATABASES,INSTALLED_APPS和SECRET_KEY.有关不是SQLite的数据库,请参考Django文档:

The settings.py file requires 3 settings: DATABASES, INSTALLED_APPS, and SECRET_KEY. Refer to Django docs for DBs that aren't SQLite:

# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'sqlite.db',
    }
}

INSTALLED_APPS = (
    'data',
    )

SECRET_KEY = 'REPLACE_ME'

真正的诀窍是在main.py中,您可以在其中对模型进行编码.显然,您必须使用wsgi来使这两行工作正常:

The real trick is in main.py, which will be where you can code against your models. Apparently you have to use wsgi to get things working with these two lines:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是示例main.py:

Here's a sample main.py:

# main.py
# Django specific settings
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

### Have to do this for it to work in 1.9.x!
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
#############

# Your application specific imports
from data.models import *

#Add user
user = User(name="someone", email="someone@example.com")
user.save()

# Application logic
first_user = User.objects.all()[0]

print(first_user.name)
print(first_user.email)

此项目以及

This project along with this post were helpful starting points for me to find the answer, and my pull request with working code for Django 1.9 was merged, so you can grab the code from masnun's repo now. If you know of a better way, please submit a pull request.

这篇关于如何在不创建Django项目的情况下使用Django 1.8.5 ORM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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