This might be a method to get an ordered list of leagues league_list = models.League.all().fetch(1000) match_list = models.Match.all().order('date').fetch(1000)
league_dict = {} for league in league_list: league.matches = [] league_dict[league.key()] = league # distribute matches to the leagues, date sorted for match in match_list: league_dict[Match.league.get_value_for_datastore(match)].matches.append(match) # don't use the reference property as a member, it will result in a datastore fetch # if league.key() == match.league.key(): def key_func(league): if not league.matches: return (sys.maxint,) # empty tuple sorts at front return tuple([datetime.toordinal(match.date) for match in league.matches]) sorted(league_list, key=key_func) 2009/5/16 June_Tutro <littlegru...@gmail.com>: > > Hi > > My app lists sports leagues with upcoming matches for some local > friendly fixtures. Usually each league will have no more than 2 to 5 > upcoming matches. Models look like this: > > class League(db.Model): > name = db.StringProperty(required=True) > contact = db.StringProperty() > > class Match(db.Model): > name = db.StringProperty(required=True) > league = db.ReferenceProperty(League) > description = db.TextProperty() > date = db.DateTimeProperty() > > In my controller I append all matches to the relevant league and send > a single data structure to the template. The template then displays > each League/Match as a single wrapped unit in a dashboard view. I want > to ensure that I always display the next upcoming match first which > means I must display its corresponding league first (leagues don't and > should not have a date entry). I have tried using the sorted() method > however cannot get it to work. > > As a league can contain multiple matches (and there me be some > overlap) I would be happy to compare the 1st upcoming match for each > league, even if say the second match in league one still comes before > the first match in league two. > > league_list = models.League.all().fetch(1000) > match_list = models.Match.all().order('date').fetch(1000) > > for match in match_list: > for league in league_list: > try: > league.match > except AttributeError: > league.matches = [] > if league.key() == match.league.key(): > league.matches.append(match) > > I thought querying by date might help in the matches, but once I > append to the ReferenceProperty league, this ordering will be lost. > Wondering what would work best? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~----------~----~----~----~------~----~------~--~---