Py学习  »  问与答

django1.7 python2.7 如何返回对象字符串而不是<Question: Question object>这样的表达式?

MrCRR • 9 年前 • 2821 次点击  

在models中创建了一个question类

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

操作API时发现 Question.objects.all()总是返回Question: Question object这样的表达式,查阅资料说python2.7的话应该加一个_unicode_方法

def _unicode_(self):
     return self.queation_text

单这样调用Question.objects.all()是还是返回object表达式,求解。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1007
 
2821 次点击  
文章 [ 3 ]  |  最新文章 9 年前
cjsundaa6
Reply   •   1 楼
cjsundaa6    9 年前
def __unicode__(self):
    return self.question_text
MrCRR
Reply   •   2 楼
MrCRR    9 年前

恩 谢谢

saibei88
Reply   •   3 楼
saibei88    9 年前

@MrCRR .all() 返回的是queryset