5

I have a Django project in Eclipse PyDev.

I have a file views.py which has the line:

from models import ingredient2

In models.py I have:

from django.db import models
class ingredient2(models.Model):
     ingredient     = models.CharField(max_length=200)

When I try to run the app I get the following error:

File "C:\Python27\lib\site-packages\django\db\models\base.py", line 54, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

I did sync the database and started the server running.

I went into base.py and added 2 print statements (yes, I probably should not edit Django's files):

if getattr(meta, 'app_label', None) is None:
            # Figure out the app_label by looking one level up.
            # For 'django.contrib.sites.models', this would be 'sites'.
            model_module = sys.modules[new_class.__module__]
            print model_module #ADDED
            print model_module.__name__ #ADDED
            kwargs = {"app_label": model_module.__name__.split('.')[-2]}

They print out:

<module 'models' from 'C:\Users\Tine\workspace\slangen\slangen2\bolig\models.pyc'>

models

manage.py is contained within the bolig folder. I think the correct app label would be "bolig". The app worked several months ago and now, when I come back to it, something is not right. I have been creating other projects in PyDev.

user984003
  • 28,050
  • 64
  • 189
  • 285
  • 1
    This may be your same problem: http://stackoverflow.com/questions/4382032/defining-a-model-class-in-django-shell-fails – AJJ Jan 07 '12 at 10:15

6 Answers6

8

Add a meta class with an app_label inside your model class definition:

class Foo:
    id = models.BigIntegerField(primary_key=True)
    class Meta:
        app_label = 'foo'
3

I had something similar

instead of

from models import ingredient2

try :

from your_app_name.models import ingredient2
wobbily_col
  • 11,390
  • 12
  • 62
  • 86
0

I was also getting the kwargs = {"app_label": model_module.__name__.split('.')[-2]} error when using PyDev. In my case, the project wasn't refreshed before I tried to run it. As soon as I refreshed it, all was well again.

Nelson
  • 49,283
  • 8
  • 68
  • 81
0

I ran into this problem using Eclipse, Django and PyDev. I needed to have the application (instead of some .py file for example) selected in the PyDev Package Explorer (left panel) before clicking Run for everything to work properly.

Zaky German
  • 14,324
  • 4
  • 25
  • 31
0

Well, not really an answer, but... I ended up creating a new django project and then copying in my code. That fixed the problem.

user984003
  • 28,050
  • 64
  • 189
  • 285
-1

in my case, models.py contains models

when I import models to other .py, say views.py it doesn't raise error when I run views.py

but when I run models.py, it raise the same error.

so I will just don't run in the models.py