jedierikb
jedierikb

Reputation: 13079

compare datetime with DateField for equivalency?

import datetime as dt 
dt.datetime.strptime( '2000', '%Y' )

creates a date object, which I can put into a django models.DateField( )

but, once the DateField is committed to the database, I am not able to check for equality with an equivalent source datetime object:

>>> dt.datetime.strptime( '2000', '%Y' ) == myRecalledDbObj.dateFieldVal
False

and printing them reveals:

1961-12-23 00:00:00
1961-12-23

So... how to compare for equivalency?

Upvotes: 1

Views: 133

Answers (1)

jterrace
jterrace

Reputation: 67063

You are comparing a datetime to a date. These will never be equal. Try calling the date method on your datetime to compare it to the DateField in django, or alternatively, switch your model to use a DateTimeField.

dt.datetime.strptime('2000', '%Y').date() == myRecalledDbObj.dateFieldVal

Upvotes: 2

Related Questions