我使用django1.6和python2.7,我的models.py中有以下内容。
class Recommend(models.Model):
id = models.IntegerField(primary_key=True)
master_id = models.IntegerField()
movie_id = models.IntegerField()
enable = models.TextField() # This field type is a guess.
class Meta:
managed = False
db_table = 'recommend'
def __unicode__(self):
return u'%s %s' % (self.id, self.master_id)
然而,结果仍然是
>>> from movies.models import Recommend
>>> Recommend.objects.all()
[<Recommend: Recommend object>]
>>>
我已查看Django tutorial unicode not working,`__unicode__()` addition not working in basic poll application in Django tutorial,Trouble with _unicode() method in django,Python 2.7__unicode__(self) not working。没有人对我有用。
答案 0 :(得分:0)
使用此
from __future__ import absolute_import, division
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Recommend(models.Model):
id = models.IntegerField(primary_key=True)
master_id = models.IntegerField()
movie_id = models.IntegerField()
enable = models.TextField() # This field type is a guess.
class Meta:
managed = False
db_table = 'recommend'
def __str__(self):
return u'%s %s' % (self.id, self.master_id)
可能应该有所帮助