我的models.py中有以下内容
import datetime
from django.utils import timezone
from django.db import models
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text 但是当我进去的时候
from polls.models import Poll, Choice
Poll.objects.all()我不明白Poll:怎么了?但是Poll: Poll对象
有什么想法吗?
发布于 2013-04-20 23:14:52
Django 1.5有对Python3的实验性支持,但Django 1.5 tutorial是为Python2.x编写的:
本教程是为Django1.5和Python2.x编写的。如果Django版本不匹配,您可以参考您的Django版本的教程或将Django更新到最新版本。如果您使用的是Python 3.x,请注意,您的代码可能需要与本教程中的代码有所不同,并且只有在您知道如何使用Python 3.x时,才应继续使用本教程。
在Python3中,您应该定义__str__方法而不是__unicode__方法。有一个装饰器python_2_unicode_compatible,它可以帮助你编写在Python2和Python3中工作的代码。
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question有关更多信息,请参阅Porting to Python 3文档中的str和unicode方法部分。
https://stackoverflow.com/questions/16121815
复制相似问题