简体   繁体   English

Python peewee,根据经过的时间过滤查询

[英]Python peewee, filtering query based on elapsed time

This is my "Request" class/Table: 这是我的“请求”类/表:

class Request(BaseModel):
    TIME_STAMP = DateTimeField(default=datetime.datetime.now)
    MESSAGE_ID = IntegerField()

With peewee, I want to select from the table, all of the "requests" that have occurred over the past 10 minutes. 对于peewee,我想从表中选择过去10分钟内发生的所有“请求”。 Something like this: 像这样:

rs = Request.select().where(Request.TIME_STAMP-datetime.datetime.now<(10 minutes))

But well, I'm not entirely sure how to get the difference between the TIME_STAMP and the current time in minutes. 但是,我不确定如何在TIME_STAMP和当前时间之间取得分钟数。

EDIT: I've tried Gerrat's suggestion, but Peewee seems to cry: 编辑:我尝试了Gerrat的建议,但Peewee似乎哭了:

/usr/local/lib/python2.7/dist-packages/peewee.py:2356: Warning: Truncated incorrect DOUBLE value: '2014-07-19 15:51:24'
  cursor.execute(sql, params or ())
/usr/local/lib/python2.7/dist-packages/peewee.py:2356: Warning: Truncated incorrect DOUBLE value: '0 0:10:0'
  cursor.execute(sql, params or ())

I've never used peewee, but if you're just subtracting two timestamps, then you'll get back a datetime.timedelta object. 我从未使用过peewee,但是如果您只减去两个时间戳,那么您将获得一个datetime.timedelta对象。 You should be able to just compare it to another timedelta object then. 然后,您应该可以将其与另一个timedelta对象进行比较。 Something like: 就像是:

rs = Request.select().where(Request.TIME_STAMP-datetime.datetime.now() 
                            <(datetime.timedelta(seconds=10*60)))

EDIT ...Looking a little closer at peewee, above may not work. 编辑 ...靠近peewee,上面的效果可能不起作用。 If it doesn't then something like the following should: 如果没有,则应如下所示:

ten_min_ago = datetime.datetime.now() - datetime.timedelta(seconds=10 * 60)
rs = Request.select().where(Request.TIME_STAMP > ten_min_ago)

It seems slightly odd that your timestamp in your database would be greater than the current time (what your select assumes)...so you may want to add the time delta and subtract the values the other way around (eg if you want to select records in the last 10 minutes). 数据库中的时间戳大于当前时间(选择所假设的时间)似乎有点奇怪...因此,您可能想要添加时间增量并以相反的方式减去值(例如,如果要选择最近10分钟内的记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM