开发者

Matching a datetime against a date in django

I have a django model with a DateTimeField called w开发者_开发百科hen which I want to match against a Date object. Is there a way to do that in django's queryset language better than

Samples.objects.filter( when__gte = mydate, when__lt = mydate + datetime.timedelta(1) )


Same like W_P, off the top of my head:

Samples.objects.filter(when__year = mydate.year, when__month = mydate.month, when__day = mydate.day)

You can round that up to year, month, day. This is the way I create posts archive in my code. I have three options: yearly archive, monthly archive and daily archive. The difference between them is the combination of arguments.


Just off the top of my head, I suppose you could do this:

def tomorrow(dt):
    return dt + datetime.timedelta(1)

# ...

Samples.objects.filter( when__gte = mydate, when__lt = tomorrow(mydate) )

I know it doesn't really *solve* your problem, but at least it looks nicer...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜