我遇到了Django Poll tutorial的问题。出于某种原因,我一直在
属性错误:'问题'对象没有属性' question_text'
我一直在按照教程进行操作,不知道出了什么问题。
Mmodels.py:
from django.db import models
import datetime
from django.utils import timezone
class Question(models.Model):
# ...
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
# ...
def __str__(self):
return self.choice_text
答案 0 :(得分:1)
我猜你的模型定义中缺少字段。根据django教程,你所指的模型看起来像这样。
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
如果放置这些,则不会遇到该错误。