django filter older than day(s)?

前端 未结 3 712
孤独总比滥情好
孤独总比滥情好 2020-12-03 02:17

The day is the number a user will input to get the result that is older than (days from user input). For example, if user inputs 32 days, they will get the results that are

相关标签:
3条回答
  • 2020-12-03 03:02

    we can use Django timezone.now() with timedelta

    from datetime import timedelta
    from django.utils import timezone
    time_threshold = timezone.now() - timedelta(days=7)
    Entry.objects.filter(entered__gte=time_threshold)
    
    0 讨论(0)
  • 2020-12-03 03:13

    Something like this would work for you:

    from datetime import datetime, timedelta
    how_many_days = 30
    MyObject.objects.filter(entered__gte=datetime.now()-timedelta(days=how_many_days))
    
    0 讨论(0)
  • 2020-12-03 03:17

    Add a timedelta(-30) to the datetime in the filter.

    0 讨论(0)
提交回复
热议问题