如何在django查询中每隔30秒从MYSQL数据库表中过滤数据?
问题描述:
我的数据库表名tripcomment
:如何在django查询中每隔30秒从MYSQL数据库表中过滤数据?
mysql> select * from orbit_tripcomments;
+----+------------------------+------------+------------+---------+---------------------+
| id | comment | citymap_id | ip_address | user_id | posted |
+----+------------------------+------------+------------+---------+---------------------+
| 2 | very nice comment 508 | 127.0.0.1 | 168 | 2014-11-20 08:25:19 |
| 3 | I make plan for visite | 508 | 127.0.0.1 | 168 | 2014-11-20 08:33:17 |
| 4 | i make a trip of manali| 508 | 127.0.0.1 | 168 | 2014-11-19 10:17:17 |
| 5 | i make a trip of manali| 508 | 127.0.0.1 | 168 | 2014-11-20 10:18:26 |
我的Django的查询是:
from datetime import datetime
from datetime import timedelta
tt = (datetime.utcnow() + timedelta(seconds=30)).strftime('%Y-%m-%d %H:%M:%S')
t_comment = models.tripcomments.objects.filter(citymap_id=parent_mapid,
posted__gte=tt)
但它返回一个空白列表。
答
你应该通过datetime
实例DateTime
场,不串:
from datetime import datetime, timedelta
results = Item.objects.filter(pub_date__lt=datetime.now() - timedelta(hours=5))
您是否正在寻找在未来的行程评论? – allcaps 2015-02-07 12:55:42