Django __unicode__仍然不适合我

时间:2014-10-13 04:55:52

标签: python django unicode

我使用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 tutorialTrouble with _unicode() method in djangoPython 2.7__unicode__(self) not working。没有人对我有用。

1 个答案:

答案 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)

可能应该有所帮助