2010-10-05 53 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

相關問題