Query datetime by today's date in Django

前端 未结 7 509
梦谈多话
梦谈多话 2021-01-31 08:16

I\'m saving datetime in the db for an object. I\'d like to query against the db and select anything from todays date, not datetime.

What\'s the

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 08:49

    in django<1.9

    from django.utils.timezone import datetime #important if using timezones
    today = datetime.today()
    foo_for_today = Foo.objects.filter(datefield__year=today.year, datefield__month=today.month, datefield__day=today.day)
    

    in django>1.9, as they added the date keyword

    foo_for_today = Foo.objects.filter(datefield__date=datetime.date.today())
    

提交回复
热议问题