简体   繁体   中英

Compare datetime values in django models

I have an Offer model that has a DateTimeField named offerDate and I have two records in the database

>>> oldOffer.offerDate
datetime.datetime(2019, 10, 29, 15, 19, 43, 755325)
>>> currOffer.offerDate
datetime.datetime(2019, 10, 29, 15, 20, 2, 456100)
>>> Offer.objects.filter(offerDate__lt= currOffer.offerDate)
<QuerySet []>
>>> Offer.objects.filter(offerDate__gt= currOffer.offerDate)
<QuerySet [<Offer: Offer object (5)>, <Offer: Offer object (6)>]>

The currOffer.offerDate is clearly greater than the oldOffer.OfferDate . Then why am I getting an empty result with the __lt clause? Also why do I get both the oldOffer (object 5) and currOffer (object 6) when I use __gt clause? What am I doing wrong?

Thanks in advance

The values of oldOffer.offerDate and currOffer.offerDate differ in Time and are equal in Date . According to the this point and the result you are getting, you may have used DateField instead of DateTimeField and the only effective part of currOffer.offerDate in comparison, is its Date .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM