Django设置没有配置错误

时间:2014-09-18 18:26:21

标签: python django python-2.7 django-settings

我试图在我的Django项目中测试模型,尽管我在运行代码时遇到错误。我使用的是Python 2.7。 错误是:

Traceback (most recent call last):
  File "PopulateModels.py", line 1, in <module>
    from django.db import models
  File "/Library/Python/2.7/site-packages/django/db/models/__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "/Library/Python/2.7/site-packages/django/db/models/deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "/Library/Python/2.7/site-packages/django/db/models/sql/__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "/Library/Python/2.7/site-packages/django/db/models/sql/subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "/Library/Python/2.7/site-packages/django/db/models/sql/aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 47, 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.

我不确定它是否有帮助,但我尝试运行程序来测试我应用的模型。因此,我尝试运行的代码位于App_Folder中,然后模型位于:App_Folder/indieapp-master/indie_app/product_extend/models.py。我不知道该怎么做才能解决这个问题。任何指导都会很棒!非常感谢!

1 个答案:

答案 0 :(得分:0)

如果你想在Python中控制它,你可以在模型中定义这个属性,因为你没有在你的配置中设置它。

文档中的示例向您展示了如何:

class TablespaceExample(models.Model):
    name = models.CharField(max_length=30, db_index=True, db_tablespace="indexes")
    data = models.CharField(max_length=255, db_index=True)
    edges = models.ManyToManyField(to="self", db_tablespace="indexes")

    class Meta:
        db_tablespace = "tables"