3

before 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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.