How do I filter by time in a date time field?

后端 未结 7 1958
野趣味
野趣味 2021-01-18 03:49

In the model \'entered\' is a datetime field. I want to query the data to find all entry\'s that where made between noon(start_time) and 5:00pm (end_time).

s         


        
相关标签:
7条回答
  • 2021-01-18 04:56

    Did you provide datetime objects for start_time and end_time?

    A quick try-out:

    class Entry(models.Model):
        entered = models.DateTimeField()
    
    >>> from datetime import datetime
    >>> Entry(entered = datetime.now()).save()
    >>> Entry.objects.filter(entered__lte = datetime.now())
    [<Entry: Entry object>]
    >>> Entry.objects.filter(entered__gte = datetime.now())
    []
    >>> Entry.objects.filter(entered__gte = datetime.now(), entered__lte=datetime(2009,11,1,0,0))
    [<Entry: Entry object>]
    
    0 讨论(0)
提交回复
热议问题