python - django: why am I getting this error: AttributeError: 'method_descriptor' object has no attribute 'today'?


I have the following python code:

from django.db import models
from datetime import datetime

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_today(self):
        return ==

In a python shell, I am trying to run:

p = Poll.objects.get(pk=1)

The first line works fine but the second line gives me this error:

AttributeError: 'method_descriptor' object has no attribute 'today'

4/17/2018 2:44:04 AM

Accepted Answer

You probably want "import datetime", not "from datetime import datetime".

"date" is a class on the datetime module, but it is also a method on the "datetime.datetime" class.

2/5/2011 8:56:31 PM

The top answer is correct, but if you don't want to import all of datetime you can write

from datetime import date

and then replace


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow