two errors in Part 2 of tutorial

41 views
Skip to first unread message

rbarh...@gmail.com

unread,
Aug 25, 2020, 6:18:23 PM8/25/20
to Django users
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.

rbarh...@gmail.com

unread,
Aug 25, 2020, 6:28:30 PM8/25/20
to Django users
I think the first serious problem is the dash in the "return self.pub_date..." line.  I don't understand at all and now I get a syntax error when I try to restart the shell...

rbarh...@gmail.com

unread,
Aug 25, 2020, 7:23:25 PM8/25/20
to Django users
Well, think I got that worked out but I have this problem with timezone, and trying to set the time to current year...
FieldError: Cannot resolve keyword 'pub_date_year' into field. Choices are: choice, id, pub_date, question_text

This doesn't seem to work:
# Get the question that was published this year.
  >>> from django.utils import timezone 
>>> current_year = timezone.now().year

Reply all
Reply to author
Forward
0 new messages