Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't compare datetime.datetime to builtin_function_or_method [closed]

I am new in Django.

I created Sponsor model that model has start_date (start date become sponsor) and end_date (end date of sponsor).

start_date = models.DateField(
        _("Start date"),
        default=datetime.date.today)

end_date = models.DateField(
        _("End date"),
        default=datetime.date.today)

I want to put all logic inside the model, if that not possible then I want to put the logic in a view. I make method current_sponsor which can return True or False (if today is on a range of start_date and end_date means True else False).

This is my current_sponsor method

def current_sponsor(self):
        today = datetime.date.today
        if today >= self.start_date:
            return True
        elif today <= self.end_date:
            return True
        else:
            return False

The problem is I got error can't compare datetime.datetime to builtin_function_or_method.

I've tried to see the data using django shell it seem works but the reality does not work.

like image 549
rischan Avatar asked May 29 '26 08:05

rischan


1 Answers

datetime.date.today is not calling the function you think it is:

>>> import datetime
>>> datetime.date.today
<built-in method today of type object at 0x7fb681a90f80>  # NOT CALLING FUNCTION

>>> datetime.date.today()  # You need () at the end
datetime.date(2015, 11, 4) 

If you add the parentheses, you'll get the result you expect.

like image 182
TayTay Avatar answered May 30 '26 22:05

TayTay