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.

1

6 Answers 6

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'
Sign up to request clarification or add additional context in comments.

Comments

3

I had something similar

instead of

from models import ingredient2

try :

from your_app_name.models import ingredient2

Comments

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.

Comments

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.

Comments

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.

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.