foo*_*yee 2 python google-app-engine
我的Feed模型中有十个实体(这是一个App Engine模型)
class Feed(db.Model):
sometext = db.StringProperty()
timestamp = db.DateTimeProperty(auto_now=True)
list_of_keys = ["key1","key2","key3".... "key10"]
Run Code Online (Sandbox Code Playgroud)
所以我使用db.key()方法调用我的实体:
feeds = db.keys(list_of_keys)
# this loop below prints the feed
for feed in feeds:
print humanizeTimeDiff(feed.timestamp)
# humanizeTimeDiff is a function to change the raw timestamp into a human friendly
# version: eg-> 10 mins ago, 20 seconds ago
Run Code Online (Sandbox Code Playgroud)
但现在,如何根据时间戳对Feed进行排序?(我希望最新的饲料位于顶部,最旧的饲料位于底部)
我可以在原始时间戳上使用的任何排序函数?(我的粗略计划是根据原始时间戳进行排序,然后将时差差异化)
PS:我不打算使用GQL查询根据时间戳查询我的实体,因为我以键列表的形式获取输入.使用db.key()是一种更快的方法.
希望我提供足够的信息.希望听到您的想法/解决方案.
Ale*_*lli 10
import operator
...
for feed in sorted(feeds, key=operator.attrgetter('timestamp'), reverse=True):
print humanizeTimeDiff(feed.timestamp)
Run Code Online (Sandbox Code Playgroud)