1

Resolved

DJANGO_SETTINGS_MODULE not defined for django management console

description

Open the tutorial project: http://download-codeplex.sec.s-msft.com/Download?ProjectName=pytools&DownloadId=783376

Create virtual env, install django

Right click on project and select Python->Open django shell

In the interactive window, try:
import polls.models
You'l get an error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".\polls\models.py", line 1, in <module>
    from django.db import models
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\sql\query.py", line 22, in <module>
    from django.db.models.sql import aggregates as base_aggregates_module
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\sql\aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\db\models\fields\__init__.py", line 116, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Users\huvalo\Downloads\tutorial\env\lib\site-packages\django\conf\__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
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.

comments

huguesv wrote Feb 7 at 10:03 PM

Workaround is to set DJANGO_SETTINGS_MODULE manually in the interactive window before attempting to import. Example:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tutorial.settings")

Zooba wrote Feb 7 at 10:05 PM

We currently do not set the DJANGO_SETTINGS_MODULE environment variable in Microsoft.PythonTools.Django.targets for the Django shell.

The workaround is to call:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'