views:

104

answers:

4

No idea why this error is popping up. Here are the models I created -

from django.db import models
from django.contrib.auth.models import User

class Shows(models.Model):
    showid= models.CharField(max_length=10, unique=True, db_index=True)
    name  = models.CharField(max_length=256, db_index=True)
    aka   = models.CharField(max_length=256, db_index=True)
    score = models.FloatField()

class UserShow(models.Model):
    user  = models.ForeignKey(User)
    show  = models.ForeignKey(Shows)

Here is the view from which I access these models -

from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User

def user_page(request, username):
    try:
        user = User.objects.get(username=username)
    except:
        raise Http404('Requested user not found.')

    shows     = user.usershow_set.all()
    template  = get_template('user_page.html')
    variables = Context({
        'username': username,
        'shows'   : shows})
    output = template.render(variables)
    return HttpResponse(output)

At this point I get an error -

OperationalError: (1054, "Unknown column 'appname_usershow.show_id' in 'field list'")

As you see this column is not even present in my models? Why this error?

A: 

Normally I get this when when I'm trying to access field which doesn't exist in Database.

Check if the field exist in the database. If you change model and perform syncdb it won't update the database, I'm not sure if that's the case.

On other note Django offers shortcut to replace try/except block in your code using get_object_or_404. (available in django.shortcuts )

try:
     user = User.objects.get(username=username)
except:
     raise Http404('Requested user not found.')

can be changed to:

user = get_object_or_404(User, username=username)
Srikanth Chundi
A: 

(After seeing the traceback posted by the OP in response to my comment)

It looks like something in the template is causing the error. Can you post the contents of the template?

Manoj Govindan
+1  A: 

Fixed it myself. Apparently any changes to the models when updated through syncdb does not change (as in update/modify) the actual tables. So I dropped the relevant DB & ran syncdb on empty DB. now it works fine :) Thanks all...

For others, 'south' data migration tool for Django seems to be fav option. Must check out...

MovieYoda