2010-01-17 46 views
2

我在饲料模型的10个实体(这是一个App Engine模型)Python列表:如何按时间戳排序? (App Engine的相关)

class Feed(db.Model): 
    sometext = db.StringProperty() 
    timestamp = db.DateTimeProperty(auto_now=True) 

list_of_keys = ["key1","key2","key3".... "key10"] 

,所以我使用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 

但是现在,如何根据时间戳对Feed进行排序? (我想最新的饲料在顶部和最旧的饲料在底部)

任何排序功能我可以在原始时间戳上使用? (我的粗略计划是根据原始时间戳进行排序,然后将时间差异人性化)

PS:我不打算使用GQL查询根据时间戳查询我的实体,因为我在表单中得到我的输入的键列表。而使用db.key()是一种更快的方法。

希望我提供了足够的信息。希望听到你的想法/解决方案。

回答

10
import operator 

    ... 

for feed in sorted(feeds, key=operator.attrgetter('timestamp'), reverse=True): 
    print humanizeTimeDiff(feed.timestamp)