Using datetime to compare with dates in Django

后端 未结 2 1198
暖寄归人
暖寄归人 2021-02-04 12:53

I have a question in Django on how you can compare dates to solve some solutions. For example I have a datefield in my models.py Like below.

class Invoice(models         


        
2条回答
  •  庸人自扰
    2021-02-04 13:46

    Use datetime.now() (notice the parens). Other than that, remember that the field will always be a datetime object. Also, (I guess that) you should check only the date of the datetime to match the current date (or else it will only match that specific second). For that you have to check if payment_date.date() == date.today() (where date is datetime.date)

    This also means that you can filter like this: Invoice.objects.filter(payment_date__lte=datetime.now()).

    __lte, __gte, __lt, __gt are used for <=, >=, < and >

提交回复
热议问题