2017-04-09 57 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

拍摄它。它的作品谢谢! :) –