Reputation: 3484
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 seem 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 })
Upvotes: 0
Views: 135
Reputation: 90007
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.
Upvotes: 1
Reputation: 3484
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 })
Upvotes: 2
Reputation: 946
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 })
Upvotes: 0