2009-05-28 8 views
3

私はちょうどPythonを学び始めたばかりで、Djangoもちょっと調べ始めました。だから私は、チュートリアルからコードのこの部分をコピー:公式のDjangoチュートリアルで立ち往生

私は貝にそれで遊んで
# Create your models here. 
class Poll(models.Model): 
    question = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published') 
    def __unicode__(self): 
     return self.question 
    def was_published_today(self): 
     return self.pub_date.date() == datetime.date.today() 

class Choice(models.Model): 
    poll = models.ForeignKey(Poll) 
    choice = models.CharField(max_length=200) 
    votes = models.IntegerField() 
    def ___unicode__(self): 
     return self.choice #shouldn't this return the choice 

、私はちょうどポーリングオブジェクトの「質問」を取得しますが、何らかの理由でそれがで戻りませんChoiceオブジェクトの「選択」私はその違いを見ることができません。シェル上の出力は次のようになります。

>>> Poll.objects.all() 
[<Poll: What is up?>] 
>>> Choice.objects.all() 
[<Choice: Choice object>, <Choice: Choice object>, <Choice: Choice object>] 
>>> 

私は、Choiceオブジェクトが "Choiceオブジェクト"以外のものを返すことを期待していました。誰が私がどこで失敗したのか、何を調べなければならないのかについてのアイデアはありますか?

編集:私を馬鹿のように感じさせる方法。はい、3つのアンダースコアが問題でした。私は約1時間それを見ていました。

+2

心配しないでくださいする必要があり、我々は愚かなエラーを作るとき、我々はすべて私たちの瞬間を持っていた;) –

答えて

8

あなたはChoiceクラスの「unicode__」の前に3アンダースコアを持っている、それはこのように、2つだけあなたの投票クラスのようにする必要があります:

def __unicode__(self): 
    return u'%s' % self.choice 
4

あなたのUnicodeの方法があまりにも多くのアンダースコアを持っています。読み込みする必要があります。

def __unicode__(self): 
    return u'%s' % self.choice 
+0

描きます! ...大丈夫、あなたはもっと速かった;-) –

3

変更:

class Choice(models.Model): 
    poll = models.ForeignKey(Poll) 
    choice = models.CharField(max_length=200) 
    votes = models.IntegerField() 
    def ___unicode__(self): 
     return self.choice #shouldn't this return the choice 

へ:

class Choice(models.Model): 
    poll = models.ForeignKey(Poll) 
    choice = models.CharField(max_length=200) 
    votes = models.IntegerField() 
    def __unicode__(self): 
     return self.choice #shouldn't this return the choice 

あなたは二__unicode__定義

2

にあまりにも多くのアンダースコアを持っていた公式のDjangoの本は少しあります時代遅れのしかし、段落へのコメントは本当に便利です。これは、2つのアンダースコアでなければなりません:

___unicode__(self): 

__unicode__(self):