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?

2
  • 2
    Why are you calling your field like a function? Commented Dec 16, 2010 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. Commented Dec 17, 2010 at 3:39

2 Answers 2

5

Typo. Should be

def was_published_today(self):
    return (self.pub_date.date() == datetime.date.today())
Sign up to request clarification or add additional context in comments.

6 Comments

Yes I had messed that up, but that didn't fix it. It still produces the same traceback.
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: Python 2.6.5 and Django 1.2.3.
@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: I just got it working. Really strange, it was(IS!) treating import datetime the same as from datetime import *.
|
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()

4 Comments

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: 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?
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: I asked it. Do you think I was clear enough? stackoverflow.com/questions/4468256/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.