Category Archives: Technology

Django : Python : Models Base.py IndexError: list index out of range

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'

ImportError: Could not import settings xxx.settings : Django : Python : Windows

Recently, I built an application using Django. I wanted to create a standalone script that used the My App’s model API to insert data into Database.

The Django Document says:

“If you’d rather not use manage.py, no problem. Just make sure mysiteandpollsare at the root level on the Python path (i.e., import mysiteandimport pollswork) and set theDJANGO_SETTINGS_MODULE environment variable tomysite.settings.”

I was little confused about those statements.

So what is PYTHONPATH? In general, it’s the collection of all searchable paths where python would search for modules that you might use in your programs.

Initially, I screwed up the environmental variables and this is the error that I got.

 

 

......self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 89, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'LearnPython.settings' (Is it on sys.path?): No module named LearnPython.settings

So this is what I did:
1) Set the PYTHONPATH environment variable to point to the directory of your django app.

PYTHONPATH = “C:\path\to\myapp\”

this is the path under which you’ll have the models.py

2) set the DJANGO_SETTINGS_MODULE environmental variable. This is the settings module that your app will use to determine the database user,password , host etc.

In my case I set DJANGO_SETTINGS_MODULE = “myapp.settings”

To confirm that everything worked, after setting the environmental variables , I logged into a new command prompt ,

C:\Users\xxx>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46).....
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['DJANGO_SETTINGS_MODULE']
cricketweet.settings
>>> from models import Player
>>>

So Now I can access the model classes from a stand alone app. Happy coding :)

Google beefs up its search with “How hot is your keyword”

Woke up with a morning surprise. Google doing more with its search. I saw google’s VP of product Meyer talking about the keyboard navigation of search results at Techcrunch Disrupt. But, “How hot is my keyword” is pretty cool than keyboard stuff (my perspective).

It gives me a perspective on what people are searching for. I typed in “Ayodhya Verdict” and this is what I got.Google_How_hot_is_my_keywordThis is pretty similar to trending topics on twitter. I wonder how useful will this be the end user. I want relevant results, I’m least bothered about how normal I’m.  Let’s see where google goes from here. They seem a little clueless.