You can fix your code by changing datetime.timedelta to timedelta:

    *def* was_published_recently(self):

        return self.pub_date >= timezone.now() - timedelta(days=1)

The statement "from datetime import datetime, timedelta" imports the
classes datetime and timedelta from the module datetime.

If you prefer to use "datetime.timedelta", you would have to use "import
datetime" instead of "from datetime import datetime, timedelta"

Hope that helps,
Daniel

On Mon, Dec 9, 2019 at 9:24 AM Bruckner de Villiers <bruck...@outlook.com>
wrote:

> Running Django 3.0 & Python 3.7.3
>
> Everything works per the tutorial until I get to:
>
> q.was_published_recently()
>
> It throws the following error:
>
> File
> "/Volumes/Data/DevelopmentTraining/django_tutorials/mysite/polls/models.py",
> line 19, in was_published_recently
>
>     return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
>
> AttributeError: type object 'datetime.datetime' has no attribute
> 'timedelta'
>
>
>
> I also tried importing timedelta from datetime – to no avail.
>
>
>
> models.py code below:
>
>
>
> import datetime
>
> from datetime import datetime, timedelta
>
>
>
> from django.db import \
>
>     models
>
>
>
> from django.utils import timezone
>
>
>
> *# 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
>
>
>
>
>
> Much obliged,
>
>
>
> Bruckner de Villiers
>
> +27 (0)83 625 1086
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/VI1PR07MB5680C26D410997944FF9A20BC8580%40VI1PR07MB5680.eurprd07.prod.outlook.com
> <https://groups.google.com/d/msgid/django-users/VI1PR07MB5680C26D410997944FF9A20BC8580%40VI1PR07MB5680.eurprd07.prod.outlook.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHEnUVXhx5C%2BwhvAoBzOq4s_SXMxqzhq_7Y5k5fCmmhEqr%2B75Q%40mail.gmail.com.

Reply via email to