Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 588
user984003 Avatar asked Jan 06 '12 07:01

user984003


2 Answers

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'
like image 167
Steve Robinson Avatar answered Jan 03 '23 05:01

Steve Robinson


I had something similar

instead of

from models import ingredient2

try :

from your_app_name.models import ingredient2
like image 34
wobbily_col Avatar answered Jan 03 '23 05:01

wobbily_col