This page has the solution to IndexError Django throws when you try to access the model API from a standalone script.
I tried to import the model class after setting the PYTHONPATH and DJANGO_SETTINGS_MODULE Environment Variables
>>> from models import Player
This is the error I got,
.....
File "C:\Python27\lib\site-packages\django\db\models\base.py", line 52, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
After some googling and code search, the base.py expects the app name and its not available when you try to access the model API from a standalone script.
So you have to add the meta information APP name to all the model classes under the models.py. That will get rid of this error.
class xxx(models.Model):
name = models.CharField(max_length=200)
country = models.CharField(max_length=200)
twitter_handle = models.CharField(max_length=100)
followers = models.IntegerField('number of twitter followers')
def __unicode__(self):
return self.twitter_handle
class Meta:
app_label = 'appname'
class yyy(models.Model):
xxx= models.ForeignKey(xxx)
conversation = models.CharField(max_length=10000)
teaser = models.CharField(max_length=50)
views = models.IntegerField('popularity')
likes = models.IntegerField()
published_date = models.DateTimeField('date_published')
def __unicode__(self):
return self.teaser
class Meta:
app_label = 'appname'
