开发者

why my django code can not be a 'Standalone Django scripts'

bef开发者_StackOverflowore you look my code , see http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/

i want to be a Standalone Django scripts'

this is my code :

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


import os
os.environ["DJANGO_SETTINGS_MODULE"] = "sphinx_test.settings"

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

setup_environ(settings)

DJANGO_SETTINGS_MODULE=sphinx_test.settings

class File(models.Model):
    name = models.CharField(max_length=200)
    tags = models.CharField(max_length=200) 

    objects = models.Manager()
    search  = SphinxQuerySet(index="test1")


import datetime


class Group(models.Model):
    name = models.CharField(max_length=32)

class Document(models.Model):
    group       = models.ForeignKey(Group)
    date_added  = models.DateTimeField(default=datetime.datetime.now)
    title       = models.CharField(max_length=32)
    content     = models.TextField()

    search      = SphinxQuerySet(File,index="test1")

    class Meta:
        db_table = 'documents'

and this is traceback:

Traceback (most recent call last):
  File "D:\zjm_code\sphinx_test\models.py", line 1, in <module>
    from django.db import models
  File "D:\Python25\Lib\site-packages\django\db\__init__.py", line 10, in <module>
    if not settings.DATABASE_ENGINE:
  File "D:\Python25\Lib\site-packages\django\utils\functional.py", line 269, in __getattr__
    self._setup()
  File "D:\Python25\Lib\site-packages\django\conf\__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.


The code that you use to set the django settings module has to come before any django-related code, including the django db imports at the top of the script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜