开发者

Unable to use sorted() on a list of Model classes

开发者_开发知识库

After running a query on the datastore, I copy the results to a new list as I interrogate, merge and prune the results. When I'm finished, I'd like to sort the new list, but I'm seeing the following error...

TypeError: 'LiveRouteStatus' object is unsubscriptable

LiveRouteStatus is a Model class that I query, and while the actual code is more complicated, here's a shortened version of what I'm doing...

class LiveRouteStatus(db.Model):
    dateAdded = db.DateTimeProperty(autho_now_add=True)
    stopID    = db.StringProperty()
    time      = db.IntegerProperty()

q = db.GqlQuery("select * from LiveRouteStatus where stopID = :1 order by dataeAdded desc limit 24", stopID)

route_results = []
for r in routes:
    if magic_test_works:
        route_results.append(r)

sorted(route_results, key=itemgetter('time')

Is there some basic element of Python that I'm screwing up here? Or is this an indexing issue with the Model class?


itemgetter('time') is like saying ['time'].

You want attrgetter('time'), which is like .time.


You are querying LiveRouteStatus and the class you declared is called LiveVehicleStatus. Not sure if this could be the reason tho !

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜