代码之家  ›  专栏  ›  技术社区  ›  richzilla

Django错误:“unicode”对象不可调用

  •  8
  • richzilla  · 技术社区  · 14 年前

    我试图从django网站上学习django教程,但我遇到了一个问题:我必须添加我的 __unicode__ 方法返回到我的模型类,但每当我尝试返回该模型的对象时,会得到以下错误:

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

    我对python很陌生,对django也很陌生,我真的看不到我在这里错过了什么,如果有人能指出的话,我会非常感激。一点点代码:

    我的模型

    # 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()
    
    1 回复  |  直到 8 年前
        1
  •  29
  •   Pi Delport    8 年前

    self.choice ()