使用django db api创建表的最简单方法是什么,并且基于“独立Django脚本” [英] what is the simplest way to create a table use django db api ,and base on 'Standalone Django scripts'

查看:154
本文介绍了使用django db api创建表的最简单方法是什么,并且基于“独立Django脚本”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以称之为独立Django表

we can call this 'Standalone Django table'

我现在不成功。

谢谢

如果您不知道独立Django脚本,请查看 http://www.b-list.org/weblog / 2007 / sep / 22 / standalone-django-scripts /

if you don't know 'Standalone Django scripts', look this http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/

2.这是我的代码:

2.this is my code:

from django.core.management import setup_environ
from sphinx_test import settings

setup_environ(settings)

import sys
sys.path.append('D:\zjm_code\sphinx_test')


from django.db import models
from djangosphinx.models import SphinxSearch,SphinxQuerySet



class File(models.Model):
    name = models.CharField(max_length=200)
    tags = models.CharField(max_length=200) 
    objects = models.Manager()
    search  = SphinxQuerySet(index="test1")
    #class Meta:#<-----------  1
    #    app_label = 'sphinx_test'#<------ 2

我的问题是当我在1和2之前添加'#'错误是:

and my problem is when i add '#' in front of 1 and 2,it's error is :

Traceback (most recent call last):
  File "D:\zjm_code\sphinx_test\books\models.py", line 17, in <module>
    class File(models.Model):
  File "D:\Python25\Lib\site-packages\django\db\models\base.py", line 52, in __new__
    kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

当我删除1和2前面的'#'时,它不打印,也不创建表。

when i remove '#' in front of 1 and 2,it print nothing,and don't create table yet.

为什么?

推荐答案

你链接的文章是一个非常好的解释最简单的方法

The article you linked is a pretty damn good explanation of the simplest way to do it.

编辑:为了清楚起见,重新排列。

Re-arranged this for clarity.

从一个新的应用程序开始,创建一个模型,同步数据库创建表,然后使用独立脚本中的 setup_environ 函数。

Starting with a fresh app, create a model, sync the database to create the tables, and then use the setup_environ function from within your standalone script.

当然这是假设 myapp 在你的 PYTHONPATH 中。如果不是,您必须在尝试导入应用程序之前附加路径:

Of course this is assuming that myapp is in your PYTHONPATH. If it isn't you must append the path to your app before try to import it:

#!/usr/bin/env python

from django.core.management import setup_environ

# If myapp is not in your PYTHONPATH, append it to sys.path
import sys
sys.path.append('/path/to/myapp/')

# This must be AFTER you update sys.path
from myapp import settings
setup_environ(settings)

from myapp.models import Foo, Bar

# do stuff
foo = Foo.objects.get(id=1)
bar = Bar.objects.filter(foo=foo.baz)

编辑#2:强>响应OP更新的代码。您正在尝试从独立脚本中创建新模型,这不是正确的方法。独立脚本不应该用于创建新的模型或应用程序,而是引用已经存在的数据。

Edit #2: In response to the updated code by the OP. You are trying to create a new model from within the standalone script, which is not the right approach. A standalone script should not be used to create new models or applications, but rather to reference already existing data.

所以使用你的例子,你需要创建一个新的应用程序在一个项目中,然后创建另一个脚本作为独立脚本。所以我将使用 standalone.py 的创建为例。

So using your example you would need to create a new app from within a project and then create another script to use as the standalone script. So I'll use the creation of standalone.py as an example.

这是什么文件结构 D:\zjm_code\sphinx_test 应该如下所示:

This is what file structure of D:\zjm_code\sphinx_test should look like:

sphinx_test
|-- __init__.py
|-- manage.py
|-- settings.py
`-- urls.py

首先,您需要从此项目文件夹中创建一个新的应用程序。让我们称之为 file_test ,并使用 python manage.py startapp file_test 创建它。现在, D:\zjm_code\sphinx_test 的文件树应如下所示:

So first you would need to create a new app from within this project folder. Let's call it file_test and create it with python manage.py startapp file_test. Now the file tree of D:\zjm_code\sphinx_test should look like this:

sphinx_test
|-- __init__.py
|-- __init__.pyc
|-- file_test
|   |-- __init__.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py
|-- manage.py
|-- settings.py
|-- settings.pyc
`-- urls.py

现在,您可以在<$ c $中创建您的文件 c> file_test\models.py :

Now you can create your File model in file_test\models.py:

from django.db import models
from djangosphinx.models import SphinxSearch,SphinxQuerySet

class File(models.Model):
    name = models.CharField(max_length=200)
    tags = models.CharField(max_length=200) 
    objects = models.Manager()
    search  = SphinxQuerySet(index="test1")
    #class Meta:#<-----------  1
    #    app_label = 'sphinx_test'#<------ 2

之后您创建此模型,您将执行 python manage.py syncdb ,以在此应用程序配置的数据库中创建模型表。

After you create this model, this is where you would execute python manage.py syncdb to create the model tables in the database you have configured for this app.

然后您可以创建具有所有逻辑的 standalone.py 来处理 file_test.models.File 模型:

Then you can create standalone.py that has all the logic to work with the file_test.models.File model you just created:

#!/path/to/python
from django.core.management import setup_environ
import sys
sys.path.append('D:\zjm_code\sphinx_test')

from sphinx_test import settings
setup_environ(settings)

# NOW you can import from your app
from sphinx_test.file_test.models import File

f = File(name='test', tags='abc,xyz,', search='foo')
f.save()

# confirm the data was saved
if f.id:
    print 'success!'
else:
    print 'fail!'

您现在已经创建了独立脚本,可以与Django ORM进行交互,而不需要运行Web服务器或测试服务器实例。这就是为什么它被认为是独立的,因为您创建的新脚本可能在命令行单独执行。

You have now created a standalone script that can interact with the Django ORM without need for a webserver or a testserver instance running. This is why it is considered to be standalone, because the new script you created may be executed at the command-line alone.

这篇关于使用django db api创建表的最简单方法是什么,并且基于“独立Django脚本”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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