2014-01-08 17 views
1

我想在pyCharm中运行我的第一个Django cassandra代码。
我的代码在Django控制台中运行平稳,但不能在.py文件中运行。这些都是错误的:我的第一个Django cassandra代码中的一些错误

C:\Python27\python.exe "D:/Developer Center/PyCharm/DJangoCassandra/MyApp/testFile.py" 
Traceback (most recent call last): 
    File "D:/Developer Center/PyCharm/DJangoCassandra/MyApp/testFile.py", line 3, in <module> 
    from MyApp.models import Person 
    File "D:\Developer Center\PyCharm\DJangoCassandra\MyApp\models.py", line 1, in <module> 
    from django.db import models 
    File "C:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module> 
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES: 
    File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__ 
    self._setup(name) 
    File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in _setup 
    % (desc, ENVIRONMENT_VARIABLE)) 
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 

Process finished with exit code 1 

这是我的.py文件:

__author__ = 'ehsan' 
from cqlengine import connection 
from MyApp.models import Person 
from cqlengine.management import sync_table,drop_table 

def main(): 
    connection.setup(['127.0.0.1:9160']) 
    sync_table(Person) 
    Person.create(id='2',name='Ali',family='Rezayee') 
    p = Person.objects.all() 
    for item in p: 
     print item.id 
    Person.filter(id='1') 

这是我的模型:

from django.db import models 
from cqlengine import Model, columns 
# Create your models here. 

class Person(Model): 
    id = columns.Text(primary_key=True) 
    name = columns.Text() 
    family = columns.Text() 
+1

你是什么意思“不在.py文件中工作”?你如何试图运行你的代码?你得到的错误是由于它无法找到你的项目的settings.py文件。它在Django控制台中工作的原因是因为当你运行'manage.py shell'时,它默认知道在哪里查找'settings.py' – ptr

回答

0

如果你在开始时managy.py shell写以下行命令,它应该工作:

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zoo.settings")