views:

35

answers:

3

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 })
A: 

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 })
jps
Hmm that works some of the time, but throws weird anomalies hmm
Emile Petrone
+2  A: 

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 })
Emile Petrone
+1  A: 

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.

Wooble