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 !
精彩评论