Sha*_*rdt 22 django python-3.x
我的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
Run Code Online (Sandbox Code Playgroud)
但是当我进入
from polls.models import Poll, Choice
Poll.objects.all()
Run Code Online (Sandbox Code Playgroud)
我没有得到民意调查:怎么了?但民意调查:民意调查对象
有任何想法吗?
Ala*_*air 38
Django 1.5对Python 3有实验支持,但Django 1.5教程是为Python 2.X编写的:
本教程是为Django 1.5和Python 2.x编写的.如果Django版本不匹配,您可以参考您的Django版本的教程或将Django更新到最新版本.如果您使用的是Python 3.x,请注意您的代码可能需要与教程中的代码不同,只有在您知道自己在使用Python 3.x时才应继续使用本教程.
在Python 3中,您应该定义__str__方法而不是__unicode__方法.有一个装饰器python_2_unicode_compatible可以帮助您编写适用于Python 2和3的代码.
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
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅移植到Python 3文档中的str和unicode方法部分.
| 归档时间: |
|
| 查看次数: |
7825 次 |
| 最近记录: |