我想在Django
中处理数据库中的一些数据。问题在于它在尝试导入models
时引发了一些异常。
# -*- coding: utf-8 -*-
from uploading import models
# lang = models.Languages.objects.get(language_shortcut='EN')
# print lang.price_per_word
我已经使用Django admin
进行迁移并插入一些数据。
SETTINGS.PY:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
这是一个例外:
Traceback (most recent call last):
File "C:/Users/Milano/PycharmProjects/mysite/uploading/scripts/calculator.py", line 3, in <module>
from uploading import models
File "C:\Users\Milano\PycharmProjects\mysite\uploading\models.py", line 3, in <module>
class Languages(models.Model):
File "C:\Users\Milano\PycharmProjects\mysite\uploading\models.py", line 4, in Languages
language = models.CharField(max_length=100)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 1081, in __init__
super(CharField, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 161, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
该代码有什么问题?
答案 0 :(得分:1)
您没有准确显示您正在做什么,但似乎您正在运行独立脚本。为了做到这一点,你需要配置Django,因为错误状态 - 请参阅the docs以获取如何执行此操作的示例。
但是请注意,这种事情通常更好地写成custom manage.py script。