0

When in the python terminal within a subfolder of my project (the subfolder that contains both my settings.py and a models.py), I perform the following:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from models import *

I get the following error:

/usr/local/lib/python2.7/dist-packages/django/db/models/base.pyc in __new__(cls, name, bases, attrs)
     90             # For 'django.contrib.sites.models', this would be 'sites'.
     91             model_module = sys.modules[new_class.__module__]
---> 92             kwargs = {"app_label": model_module.__name__.split('.')[-2]}
     93         else:
     94             kwargs = {}

IndexError: list index out of range

This is the same error seen in Defining a model class in Django shell fails but here I have the models defined in a project. What is the problem?

Community
  • 1
  • 1
Zags
  • 37,389
  • 14
  • 105
  • 140

1 Answers1

0

The problem is that the import (and consequently python shell) must be run from the root of the django project. Otherwise, django does not have the module name, which is part of the database path.

Zags
  • 37,389
  • 14
  • 105
  • 140