Tuesday, August 25, 2020

Re: two errors in Part 2 of tutorial

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

On Tuesday, August 25, 2020 at 3:28:30 PM UTC-7 rbarh...@gmail.com wrote:
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...


On Tuesday, August 25, 2020 at 3:18:23 PM UTC-7 rbarh...@gmail.com wrote:
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.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3247bf50-bd0e-418a-a47d-889ab112e429n%40googlegroups.com.

No comments:

Post a Comment