django filter by datetime on a range of dates

后端 未结 5 1070
我寻月下人不归
我寻月下人不归 2021-01-01 23:55

I have a model with field \"created_at\", and I have a list of dates. So, I want to get all the models that are created in the date range. How ?

I know that we ca

相关标签:
5条回答
  • 2021-01-02 00:31

    Use https://pypi.python.org/pypi/django-daterange-filter/ to use date range filter.

    0 讨论(0)
  • 2021-01-02 00:32

    You can combine date and range field lookups:

    queryset.filter(created_at__date__range=(start_date, end_date))
    
    0 讨论(0)
  • 2021-01-02 00:39

    You can use __gte (greater than or equal) and __lte (less than or equal). For example:

    queryset.filter(created_at__gte=datetime.date.today())
    
    0 讨论(0)
  • 2021-01-02 00:40

    You can use range lookup. Just find the lowest and greater date and then apply it as:

    queryset.filter(created_at__range=(start_date, end_date))
    
    0 讨论(0)
  • 2021-01-02 00:47

    You can use range e.g.

    first_date = datetime.date(2005, 1, 1)
    last_date = datetime.date(2005, 3, 31)
    queryset.filter(created_at__range=(first_date, last_date))
    
    0 讨论(0)
提交回复
热议问题