I had a problem with the Django tutorial so I asked a question here. No-one knew the answer, but I eventually figured it out with help from Robert. Python seems to be treating import datetime
the same as from datetime import *
.
Working code:
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()
Not working code: (The only differences are the import statements and the last line.)
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())
EDIT: I guess I wasn't clear enough. The code produces the exact same traceback with the last line being return (self.pub_date.date() == datetime.date.today())
Me originally forgetting to add .date()
is NOT the error I'm asking about.
The traceback produced by the not working code:
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
Why on earth is it doing this?
My question is NOT about forgetting .date()
. My question is: Why is datetime in my namespace without me using from datetime import *
.
Note: The first question asked what it was doing. This question asks why.
UPDATE: Suddenly it works. With datetime.date.today()
AND date.today()
. My question remains though, why does date.today()
work? It seems datetime is in my local namespace without me putting there. Why?