3

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.

TayTay
  • 6,882
  • 4
  • 44
  • 65
rischan
  • 1,553
  • 13
  • 19
  • 3
    ...you're missing parentheses. – jonrsharpe Nov 04 '15 at 13:52
  • 1
    @rischan No offense, but it would _really_ benefit you to start actually reading the exception messages... They couldn't be any clearer, but if they're not a simple search will make it so; these errors you got have been asked a million times. – Agustín Lado Nov 04 '15 at 15:43

1 Answers1

6

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
TayTay
  • 6,882
  • 4
  • 44
  • 65
  • Thanks @jonrshape Now I realized that I miss parentheses, but when I put parentheses I am still get error `can't compare datetime.datetime to datetime.date`. – rischan Nov 04 '15 at 14:29
  • 1
    As the exception says, one of the dates you're comparing is a `datetime.datetime` object and one is a `datetime.date` object. See [this answer](http://stackoverflow.com/questions/7239315/cant-compare-datetime-datetime-to-datetime-date). – TayTay Nov 04 '15 at 14:33
  • I changed everything to `today = datetime.datetime.today()` and now I get different error `can't compare offset-naive and offset-aware datetimes ` – rischan Nov 04 '15 at 14:55
  • @rischan this is a Django "feature." I'm going to assume you're using Django 1.4... check [this link](http://stackoverflow.com/questions/10652819/django-1-4-cant-compare-offset-naive-and-offset-aware-datetimes) which discusses your exact issue. – TayTay Nov 04 '15 at 14:58
  • 2
    Thanks Tgsmith61591 it because of utc. I've replace it with `replace(tzinfo=utc):` and it works. – rischan Nov 04 '15 at 15:00