Reputation: 70
I got a field error when I was trying to run the tutorial of Django Part 2. I'm using Python 3.6 and 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
Working with the tutorial, step by step. I was running the 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')
I got the following error after 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
I don't know what to do to repair this error, maybe I put something wrong at models, that's why I share models.py
with you. I hope you can help me
Upvotes: 1
Views: 1045
Reputation: 754
You should have 2 underscores before startswith.
Try
Question.objects.filter(question_text__startswith='What')
Upvotes: 4