GAE 递归查询

GAE Recursive Query

我有一些代码(如下)产生了有效的结果,总的来说我很满意。但是,它相当 'wordy',我很想知道这种 good/bad 方法是否还有更多 effective/simpler 我应该做的事情?

我很高兴模型中有代码而不是我的 api,所以我想保持这种状态。

class ndb_Project(ndb.Model):
    name = ndb.StringProperty()
    description = ndb.StringProperty()
    version = ndb.StructuredProperty(ndb_Version)
    parentProj = ndb.KeyProperty()
    childProj = ndb.KeyProperty(repeated=True)

    @classmethod
    def getChildProjects(cls,key):
        proj = key.get()
        a = []
        for c in proj.childProj:
            a.append(proj.getChildProjects(c))
        o = proj.to_dict()
        o['urlSafe'] = proj.key
        o['childProj'] = a
        return o

非常感谢 托比


替代方案?

@classmethod
    def getChildProjects(cls, proj):
        o = proj.to_dict()
        o['urlSafe'] = proj.key
        temp = []
        a = ndb.get_multi(proj.childProj) if proj.childProj else []
        for c in a:
             temp.append(proj.getChildProjects(c))
        o['childProj']
        return o

像这样的东西应该使用 ndb.get_multi()。它也会更快,因为它在单个 rpc 中获取多个密钥,而不是像当前循环那样串行获取。

编辑 我在 /_ah/stats/shell 中 运行 所以我知道只要 returns 你想要的输出它就可以工作。

from google.appengine.ext import ndb
from pprint import pprint as pp


class ndb_Project(ndb.Model):
    name = ndb.StringProperty()
    description = ndb.StringProperty()
    parentProj = ndb.KeyProperty()
    childProj = ndb.KeyProperty(repeated=True)

    def getChildProjects(self):
        a = [ent.getChildProjects() for ent in ndb.get_multi(self.childProj)]
        o = self.to_dict()
        o['urlSafe'] = self.key
        o['childProj'] = a
        return o

p = ndb_Project(name='first', description='first entity')
p.put()

for x in xrange(5):
    tmp = ndb_Project(name=str(x), description='desc %s' % x)
    p.childProj.append(tmp.put())
    for y in xrange(5):
        tmp2 = ndb_Project(name='%s %s' % (x, y), description='desc %s %s' % (x, y))
        tmp.childProj.append(tmp2.put())
    tmp.put()
p.put()

pp(p.getChildProjects())

输出

{'childProj': [{'childProj': [{'childProj': [],
                               'description': u'desc 0 0',
                               'name': u'0 0',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5251250308251648)},
                              {'childProj': [],
                               'description': u'desc 0 1',
                               'name': u'0 1',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5245618633048064)},
                              {'childProj': [],
                               'description': u'desc 0 2',
                               'name': u'0 2',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5793979555643392)},
                              {'childProj': [],
                               'description': u'desc 0 3',
                               'name': u'0 3',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6371518539890688)},
                              {'childProj': [],
                               'description': u'desc 0 4',
                               'name': u'0 4',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5208529711398912)}],
                'description': u'desc 0',
                'name': u'0',
                'parentProj': None,
                'urlSafe': Key('ndb_Project', 6460121467060224)},
               {'childProj': [{'childProj': [],
                               'description': u'desc 1 0',
                               'name': u'1 0',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6334429618241536)},
                              {'childProj': [],
                               'description': u'desc 1 1',
                               'name': u'1 1',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6344957925261312)},
                              {'childProj': [],
                               'description': u'desc 1 2',
                               'name': u'1 2',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6448551898906624)},
                              {'childProj': [],
                               'description': u'desc 1 3',
                               'name': u'1 3',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5286432096649216)},
                              {'childProj': [],
                               'description': u'desc 1 4',
                               'name': u'1 4',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5808107145920512)}],
                'description': u'desc 1',
                'name': u'1',
                'parentProj': None,
                'urlSafe': Key('ndb_Project', 5282008817205248)},
               {'childProj': [{'childProj': [],
                               'description': u'desc 2 0',
                               'name': u'2 0',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5808889098403840)},
                              {'childProj': [],
                               'description': u'desc 2 1',
                               'name': u'2 1',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5035775120900096)},
                              {'childProj': [],
                               'description': u'desc 2 2',
                               'name': u'2 2',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5881052870475776)},
                              {'childProj': [],
                               'description': u'desc 2 3',
                               'name': u'2 3',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5850614068150272)},
                              {'childProj': [],
                               'description': u'desc 2 4',
                               'name': u'2 4',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5215350924771328)}],
                'description': u'desc 2',
                'name': u'2',
                'parentProj': None,
                'urlSafe': Key('ndb_Project', 5811114428334080)},
               {'childProj': [{'childProj': [],
                               'description': u'desc 3 0',
                               'name': u'3 0',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6368218830602240)},
                              {'childProj': [],
                               'description': u'desc 3 1',
                               'name': u'3 1',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5287664114728960)},
                              {'childProj': [],
                               'description': u'desc 3 2',
                               'name': u'3 2',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5041177015353344)},
                              {'childProj': [],
                               'description': u'desc 3 3',
                               'name': u'3 3',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6412332003491840)},
                              {'childProj': [],
                               'description': u'desc 3 4',
                               'name': u'3 4',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5231029602222080)}],
                'description': u'desc 3',
                'name': u'3',
                'parentProj': None,
                'urlSafe': Key('ndb_Project', 5245939144982528)},
               {'childProj': [{'childProj': [],
                               'description': u'desc 4 0',
                               'name': u'4 0',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 4964143656337408)},
                              {'childProj': [],
                               'description': u'desc 4 1',
                               'name': u'4 1',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 4927054734688256)},
                              {'childProj': [],
                               'description': u'desc 4 2',
                               'name': u'4 2',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 6348349238149120)},
                              {'childProj': [],
                               'description': u'desc 4 3',
                               'name': u'4 3',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 5004957119938560)},
                              {'childProj': [],
                               'description': u'desc 4 4',
                               'name': u'4 4',
                               'parentProj': None,
                               'urlSafe': Key('ndb_Project', 4960843947048960)}],
                'description': u'desc 4',
                'name': u'4',
                'parentProj': None,
                'urlSafe': Key('ndb_Project', 5250989925859328)}],
 'description': u'first entity',
 'name': u'first',
 'parentProj': None,
 'urlSafe': Key('ndb_Project', 5208229332123648)}

原始代码 以便对这个答案的原始评论有意义

class ndb_Project(ndb.Model):
    name = ndb.StringProperty()
    description = ndb.StringProperty()
    version = ndb.StructuredProperty(ndb_Version)
    parentProj = ndb.KeyProperty()
    childProj = ndb.KeyProperty(repeated=True)

    @classmethod
    def getChildProjects(cls, key):
        proj = key.get()
        a = ndb.get_multi(proj.childProj) if proj.childProj else []
        o = proj.to_dict()
        o['urlSafe'] = proj.key
        o['childProj'] = a
        return o