Here is my code from working on Part 2 of the first Django app
~~~
from django.db import models
from django.utils import timezone
import datetime
# Create your models here.
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
~~~
I get the following error to the following questions:
~~~
In [9]: Question.objects.filter(question_text_startswith='What')
FieldError: Cannot resolve keyword 'question_text_startswith' into field. Choices are: choice, id, pub_date, question_text
In [12]: Question.objects.get(pub_date_year=current_year)
FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: choice, id, pub_date, question_text
q.was_published_recently()
AttributeError: 'Question' object has no attribute 'pub'
~~~
Plus, I don't understand the "-" in this line:
return self.pub_date >= timezone.now() -
datetime.timedelta(days=1)
Help, please. Thank you.