GAE递归查询

时间:2015-10-09 13:59:07

标签: python-2.7 google-app-engine google-cloud-datastore app-engine-ndb

我有一些代码(下面)正在生成有效的结果,总体而言我很满意。但是,它非常“罗嗦”。而且我很想知道这种好/坏方法 - 这有什么更有效/更简单我应该做的吗?

我很高兴在模型中使用代码,而不是在我的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

1 个答案:

答案 0 :(得分:0)

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

编辑我在/_ah/stats/shell中运行了这个,所以我知道只要它返回你想要的输出就行了。

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
相关问题