Select between two dates with Django

后端 未结 4 2071
春和景丽
春和景丽 2020-12-04 13:35

I am looking to make a query that selects between dates with Django.

I know how to do this with raw SQL pretty easily, but how could this be achieved using the Djang

相关标签:
4条回答
  • 2020-12-04 14:29

    __range

    0 讨论(0)
  • 2020-12-04 14:34

    two methods

    .filter(created_at__range=[from_date, to_date])
    

    another method

    .filter(Q(created_at__gte=from_date)&Q(created_at__lte=to_date))
    
    • gte means greater than equal
    • lte means less than equal
    0 讨论(0)
  • 2020-12-04 14:39

    Use the __range operator:

    ...filter(current_issue__isnull=True, created_at__range=(start_date, end_date))
    
    0 讨论(0)
  • 2020-12-04 14:40

    If you are using a DateTimeField, Filtering with dates won’t include items on the last day.

    You need to casts the value as date:

    ...filter(created_at__date__range=(start_date, end_date))
    
    0 讨论(0)
提交回复
热议问题