Django filter events occurring today

前端 未结 6 711
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 09:56

I\'m struggling to logically represent the following in a Django filter. I have an \'event\' model, and a location model, which can be represented as:

class          


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 10:26

    timezone.localtime(timezone.now()).date() gets you the correct date.

    To get events occurring today(start today):

    from django.utils import timezone
    
    class EventManager(models.Manager):
        def bookings_today(self, location_id):
            t = timezone.localtime(timezone.now())
            bookings = self.filter(location=location_id, start__year = t.year,
                start__month = t.month, start__day = t.day, )
    

提交回复
热议问题