2010-10-05 104 views
8

即时尝试从django网站上做django教程,并且我遇到了一些问题:我必须将我的__unicode__方法添加到我的模型类中,但是当我试图返回该模型我得到以下错误的对象:django错误:'unicode'对象不可调用

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

IM相当新的Python和很新的Django的,我真的不能看什么香港专业教育学院错过这里,如果有人能指出来标识非常感谢。代码位:

我的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() 

,并在交互shell:

from pysite.polls.models import Poll, Choice 
Poll.objects.all() 

回答

29

self.choice是一个字符串值,但代码试图调用它像一个功能。之后删除()

+0

现货,谢谢你的帮助。 – richzilla 2010-10-05 18:40:21

+0

正是我需要知道的另一种情况。谢谢。 – ihightower 2012-09-17 15:12:58

相关问题