2009-05-28 5 views
3

나는 방금 Python을 배우기 시작했고 장고에 대해서도 약간 알아보기 시작했다. 그래서 튜토리얼에서이 코드 조각을 복사 :공식 장고 자습서에 붙어

내가 쉘에 놀러
# 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 객체"이외의 다른 값을 반환 할 것으로 예상했습니다. 아무도 내가 실패한 부분과 내가 조사해야 할 부분에 대한 아이디어가 있습니까?

편집 : 나를 바보처럼 느끼게하는 방법. 그렇습니다. 세 가지 밑줄이 문제였습니다. 나는 약 1 시간 동안 그것을보고 있었다.

+2

을해야, 우리가 바보 같은 실수를 할 때 우리는 우리의 순간이 있었다 모든왔다 –

답변

8

당신은 선택 클래스의 "unicode__"전에 세 밑줄을 가지고,이 같은 설문 조사 클래스에서 두 개의 등이 있어야한다 :

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

귀하의 유니 코드 방식은 너무 많은 밑줄이 있습니다. 읽어야합니다

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

에 너무 많은 밑줄이 있었다 공식 장고 책은 조금입니다 오래된. 그러나 단락에 대한 주석은 정말 유용합니다.

___unicode__(self): 

걱정하지 마십시오 __unicode__(self):