[python]python 2とpython 3の互換性


@python_を使用できます2_unicode_compatible
例:
元のコード
# coding:utf-8
from django.db import models
 
 
class Article(models.Model):
    title = models.CharField(u'  ', max_length=256)
    content = models.TextField(u'  ')
 
    pub_date = models.DateTimeField(u'    ', auto_now_add=True, editable = True)
    update_time = models.DateTimeField(u'    ',auto_now=True, null=True)
 
    def __unicode__(self):#  Python3   __str__    __unicode__
        return self.title

に改心
# coding:utf-8
from __future__ import unicode_literals
 
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
 
@python_2_unicode_compatible
class Article(models.Model):
    title = models.CharField('  ', max_length=256)
    content = models.TextField('  ')
 
    pub_date = models.DateTimeField('    ', auto_now_add=True, editable = True)
    update_time = models.DateTimeField('    ',auto_now=True, null=True)
 
    def __str__(self):
        return self.title

python_2_unicode_compatibleはpythonの異なるバージョンに適応するために自動的にいくつかの処理を行います.この例のunicode_literalsはpython 2.xはpython 3のようにunicode文字を処理し、互換性を向上させる.
参考リンク:クリックしてリンクを開く
@python_2_unicode_compatible