如何在django查询中每30秒过滤一次来自MYSQL数据库表的数据?

时间:2015-02-07 11:32:13

标签: django

我的数据库表名是tripcomment

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)

但它返回一个空白列表。

1 个答案:

答案 0 :(得分:0)

您应该为datetime字段传递DateTime个实例,而不是字符串:

from datetime import datetime, timedelta

results = Item.objects.filter(pub_date__lt=datetime.now() - timedelta(hours=5))