John
John

Reputation: 16058

django datetime.datetime error

While following the tutorial here, I get down to where you run poll.was_published_today and I get this error:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/myDir/mySite/polls/models.py", line 11, in was_published_today
    return (self.pub_date() == datetime.date.today())
TypeError: 'datetime.datetime' object is not callable

Here is the code for my poll class:

from django.db import models
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 (self.pub_date() == datetime.date.today())

I've tried a few different things and it always chokes on any mention of "datetime".

This code:

import datetime
datetime.date.today()

when run in the interpreter works fine, just as expected, but in my file, it doesn't. Any suggestions?

Upvotes: 2

Views: 5496

Answers (2)

John
John

Reputation: 16058

I fixed it. For some reason it's treating import datetime like from datetime import * (Anyone know why?) So removing datetime from

return (self.pub_date.date() == datetime.date.today())

corrected it. I also decided to import datetime first though I don't know if that did anything.

The working file(for me) is:

import datetime
from django.db import models

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 self.pub_date.date() == date.today()

Upvotes: 2

Robert
Robert

Reputation: 6540

Typo. Should be

def was_published_today(self):
    return (self.pub_date.date() == datetime.date.today())

Upvotes: 5

Related Questions