2

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?

John
  • 15,418
  • 12
  • 44
  • 65
  • 2
    Why are you calling your field like a function? – Ignacio Vazquez-Abrams Dec 16 '10 at 22:55
  • @Ingacio: Doh! This is why I come here. So I don't spend even *more* hours working on a bug the first other set of eyes sees. While I did mess that up, it didn't fix it. It still produces the same traceback. – John Dec 17 '10 at 03:39

2 Answers2

5

Typo. Should be

def was_published_today(self):
    return (self.pub_date.date() == datetime.date.today())
Robert
  • 6,412
  • 3
  • 24
  • 26
  • Yes I had messed that up, but that didn't fix it. It still produces the same traceback. – John Dec 17 '10 at 03:38
  • I don't understand. When I did the tutorial, I had the exact same code. I just tested it again and it still works. I guess, what Python version and what Django version are you using? – Robert Dec 17 '10 at 04:01
  • @Robert: Python 2.6.5 and Django 1.2.3. – John Dec 17 '10 at 05:26
  • @John - I'm stumped. I'm running those versions, with the same code (literally! I did the same tutorial last month), and have no problem. – Robert Dec 17 '10 at 05:43
  • @Robert: I just got it working. Really strange, it was(IS!) treating `import datetime` the same as `from datetime import *`. – John Dec 17 '10 at 05:59
  • You're still the accepted answer though because you *did* fix *one* of my problems, just not the one I asked the question about! :D Thanks! – John Dec 17 '10 at 06:01
2

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()
John
  • 15,418
  • 12
  • 44
  • 65
  • 1
    I'm absolutely flabbergasted. I've *NEVER* seen Python do that. There's a reason the `from` syntax is so different - it's to avoid namespace pollution. You probably still have a problem, but I'm glad you've gotten it working. – Robert Dec 17 '10 at 06:09
  • @Robert: My thoughts *exactly*. Do you think I can ask a question why it's doing this or will it get closed as a dupe of this? – John Dec 17 '10 at 06:14
  • Eh... well it effectively *is* a dupe, now that we've figured out what it was... but you're well off the 'newest' list by now. Make sure to reference this question so you're upfront about it – Robert Dec 17 '10 at 06:16
  • 1
    @Robert: I asked it. Do you think I was clear enough? http://stackoverflow.com/questions/4468256/import-module-from-module-import – John Dec 17 '10 at 06:25