Codo
Codo

Reputation: 271

Can't import django.db.models from Python GUI

Whenever I type this command in Python GUI:

from django.db import models

it gives me an error like this

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\models\__init__.py", line 5, in <module>
    from django.db.models.query import Q
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 17, in <module>
    from django.db.models.deletion import Collector
  File "C:\Python27\lib\site-packages\django\db\models\deletion.py", line 4, in <module>
    from django.db.models import signals, sql
  File "C:\Python27\lib\site-packages\django\db\models\sql\__init__.py", line 4, in <module>
    from django.db.models.sql.subqueries import *
  File "C:\Python27\lib\site-packages\django\db\models\sql\subqueries.py", line 12, in <module>
    from django.db.models.sql.query import Query
  File "C:\Python27\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:\Python27\lib\site-packages\django\db\models\sql\aggregates.py", line 9, in <module>
    ordinal_aggregate_field = IntegerField()
  File "C:\Python27\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:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
    self._setup(name)
  File "C:\Python27\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.

What can it be the problem? I have django in directory C:\Python27\Lib\site-packages\django\db and in there i have a file called models.

This is the pic

enter image description here

Bizarre isn't it?

Upvotes: 0

Views: 1094

Answers (2)

Erik Kaplun
Erik Kaplun

Reputation: 38227

Not all of Django supports simply being loaded as a Python module, unfortunately; you need to use the means provided by Django itself to bootstrap the environment needed to load stuff like models.

One option is to use the Django shell but it's also possible to use a purely programmatic solution from your own code. See https://docs.djangoproject.com/en/dev/topics/settings/ for all the options.

Here's also an example by somebody else https://gist.github.com/jordanorelli/1025419 but I haven't verified if it's up to date.

Upvotes: 1

Anentropic
Anentropic

Reputation: 33883

The answer is in the error message:

You must either define the environment variable DJANGO_SETTINGS_MODULE or...

For this reason Django provides a shortcut to loading a python shell with Django settings configured:

https://docs.djangoproject.com/en/dev/ref/django-admin/#shell

Upvotes: 0

Related Questions