views:

26

answers:

2

I have the following models:

class Station(db.Model):
    code = db.StringProperty(required=True)
    name = db.StringProperty(required=True)

class Schedule(db.Model):
    tripCode = db.StringProperty(required=True)
    station = db.ReferenceProperty(Station, required=True)    
    arrivalTime = db.TimeProperty(required=True)
    departureTime = db.TimeProperty(required=True)

How can I order programatically all the Schedules by Station's name?

Something like Schedule.all().order('station.name')

A: 

You will to de-normalize your models or sort the results in memory:

Schedule.all().fetch(100).sort(key=lambda s: s.station.name)

(code not tested)

jbochi
all() is a Query, not a list. It can't be sorted.
Wooble
A: 

After use sort i think you need to fetch all entities:

Schedule.all().fetch (100).sort(key=lambda s: s.station.name)

May be you can also use collection name. http://goo.gl/jXEk But i think the jbochi answer is better :)

[x.schedule_set.get () for x in Station.all ().order ('name')]
sahid
@jbochi: Unfortunately as wooble says, fetch returns a Query so isn't sortable. @Sahid: this list only returns the first shedule and station of every station, to return all the schedules it can be something like [y.fetch(1000) for y in [x.schedule_set.get () for x in Station.all ().order ('name')] that returns a list of list
pvilas