2017-04-09 56 views
-2

我首先記下了使對象更易於人工讀取的方法,但在cmd中運行我的對象時它不起作用。就在我運行內置API的django之後。 python manage.py shell。然而,當我運行Question.objects.all()時,它仍然給出了這個結果,它返回<QuerySet [Question: Question object]>我的結果應該返回<QuerySet [<Question: What's up?>]>。請幫我解決這個問題。無法實現_str_()方法並使其在cmd中運行

import datetime 
from django.db import models 
from django.utils import timezone 


# Create your models here. 
class Question(models.Model): 
question_text = models.CharField(max_length=200) 
pub_date = models.DateTimeField('date published') 
def _str_(self): 
    return self.question_text 



    class Choice(models.Model): 
question = models.ForeignKey(Question, on_delete=models.CASCADE) 
choice_text = models.CharField(max_length=200) 
votes = models.IntegerField(default=0) 
def _str_(self): 
    return self.choice_text 
+0

'__str__'不是'_str_'。 –

回答

0

替換:

def _str_(self): 
    ..... 

有:

def __str__(self): 
    ..... 

雙下劃線,而不是單一的,。

+0

我試過,但是當我跑了它給我NameError:名稱'問題沒有定義' –

+0

拍攝它。它的作品謝謝! :) –