Django教程第2部分字段错误
当我试图运行Django第2部分的教程时,出现了一个字段错误。我正在使用Python 3.6和Django 1.11 models.pyDjango教程第2部分字段错误,django,Django,当我试图运行Django第2部分的教程时,出现了一个字段错误。我正在使用Python 3.6和Django 1.11 models.py import datetime from django.db import models from django.utils.encoding import python_2_unicode_compatible from django.utils import timezone class Question(models.Model): quest
import datetime
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils import timezone
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
逐步使用教程。我正在运行python manage.py shell
:
>>> from polls.models import Question, Choice
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(id=1)
<QuerySet [<Question: What's up?>]>
>>> Question.objects.filter(question_text__startswith='What')
我不知道如何修复此错误,可能是我在模型上出错了,这就是我与您共享
models.py
的原因。我希望你能帮我在开始之前,你应该有两个下划线
试一试
错误明确指出,
startswith
前面没有两个下划线。检查此项。'question\u text\u startswith'
应该是'question\u text\u startswith'
谢谢,我在那里呆了一会儿:)
django.core.exceptions.FieldError: Cannot resolve keyword 'question_text_startswith' into field. Choices are: choice, id, pub_date, question_text
Question.objects.filter(question_text__startswith='What')