def get(self): u"""アクティビティを返却する.""" def _groupby_user(stage_user): user = stage_user.user.get() return {'screenName': user.screenName, 'image': user.image} def _stageuser_to_dict(obj): return [{'clearDate': str(o.clearDate), 'stageNo': o.stage.get().stageNo} for o in obj] stageUsers = StageUser.gql('ORDER BY clearDate DESC').fetch(limit=50) from itertools import groupby activities = [{'user': user, 'list': _stageuser_to_dict(stageusers)} for user, stageusers in groupby(stageUsers, _groupby_user)] self.response.out.write(json.dumps(activities))