Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django error: 'unicode' object is not callable

Tags:

python

django

im attempting to do the django tutorial from the django website, and ive run into a bit of an issue: ive got to adding my __unicode__ methods to my models classes, but when ever i try to return the objects of that model i get the following error:

in __unicode__
    return self.question()
TypeError: 'unicode' object is not callable

im fairly new to python and very new to django, and i cant really see what ive missed here, if someone could point it out id be very grateful. A bit of code:

My models.py:

# The code is straightforward. Each model is represented by a class that subclasses django.db.models.Model. Each model has a number of 
# class variables, each of which represents a database field in the model.

from django.db import models

    class Poll(models.Model):
        question = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published') 

        def __unicode__(self):
            return self.question


    class Choice(models.Model):
        poll = models.ForeignKey(Poll) 
        choice = models.CharField(max_length=200) 
        votes = models.IntegerField()

        def __unicode__(self):
            return self.choice()

and in the interactive shell:

from pysite.polls.models import Poll, Choice
Poll.objects.all()
like image 823
richzilla Avatar asked Oct 05 '10 18:10

richzilla


1 Answers

self.choice is a string value, but the code is trying to call it like a function. Just remove the () after it.

like image 104
Pi Delport Avatar answered Oct 28 '22 10:10

Pi Delport