Django教程第2部分字段错误

时间:2018-03-12 15:07:56

标签: 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):
    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')

Question.objects.filter(question_text__startswith='What')之后我收到了以下错误:

django.core.exceptions.FieldError: Cannot resolve keyword 'question_text_startswith' into field. Choices are: choice, id, pub_date, question_text

我不知道如何修复此错误,也许我在模型上出错了,这就是我与你分享models.py的原因。我希望你能帮助我

1 个答案:

答案 0 :(得分:3)

在开始之前你应该有2个下划线。

尝试

Question.objects.filter(question_text__startswith='What')
相关问题