开发者

Display GAE list by most recent entry

n00b problem- I am trying to have a list show the most recent entry first. This works without the reverse(), but retrieves nothing with it in. I have heard I should try and use order_by(), but I can't s开发者_运维知识库eem to get that to work either. Thanks for the help!

class MainHandler(webapp.RequestHandler):

def get(self):

    que = db.Query(models.URL)
    url_list = que.fetch(limit=100)
    new_list = url_list.reverse()

    path = self.request.path        
    if doRender(self,path):
        return
    doRender(self,'base/index.html', { 'new_list' : new_list })


In django, you use order_by(), but for GAE it is order().

So the answer was not in using reverse but:

class MainHandler(webapp.RequestHandler):

def get(self):

    que = db.Query(models.URL).order('-created')
    url_list = que.fetch(limit=100)

    path = self.request.path        
    if doRender(self,path):
        return
    doRender(self,'base/index.html', { 'url_list' : url_list })


Keys aren't automatically in incrementing order; if you want to sort by the date an entity was added, you need to add a DateTimeProperty with auto_now_add set to True, and sort on that.


reverse() modifies the list in place. It doesn't return the sorted list, so just do:

url_list.reverse()
path = self.request.path        
if doRender(self,path):
    return
doRender(self,'base/index.html', { 'new_list' : url_list })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜