user984003
user984003

Reputation: 29527

Django Models "IndexError: list index out of range" Pydev

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.

Upvotes: 5

Views: 4934

Answers (6)

wobbily_col
wobbily_col

Reputation: 11879

I had something similar

instead of

from models import ingredient2

try :

from your_app_name.models import ingredient2

Upvotes: 3

beginner
beginner

Reputation: 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

Upvotes: -1

Zaky German
Zaky German

Reputation: 14334

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.

Upvotes: 0

Steve Robinson
Steve Robinson

Reputation: 81

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'

Upvotes: 8

Anonymous Joe
Anonymous Joe

Reputation: 101

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.

Upvotes: 0

user984003
user984003

Reputation: 29527

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

Upvotes: 0

Related Questions