親が子を後援している場合、ノードが子であるツリーである階層組織があります。このコードでツリーをトラバースできるようです
def get_team(self, person, team):
firstline = User.query(User.sponsor == person.key).fetch(99999999)
if firstline:
for person in firstline:
team.append(person)
newdownline = self.downline(person, team)
return team
上記を使用して、私はユーザーの組織を取得することができます
downline=user.get_team(user, [])
しかし、1つのリクエストに対してこれを何度も実行する必要があり、その多くの再帰は非効率的である可能性があるため、より効率的な方法はありますか?または、ツリーを正しくトラバースできるため、コードは問題ありませんか?私の最初のバージョンでは、3つの変数を使用しましたが、コードを次の代わりに2つの変数に再配置できることがわかりました。
def downline(self, person, team, teamlist):
firstline = User.query(User.sponsor == person.key).fetch(99999999)
if firstline:
for person in firstline:
teamlist.append(person)
newdownline = self.downline(person, team, teamlist)
team.append(newdownline)
return teamlist
teamlist変数は実際には必要ないことがわかったので、削除しました。私がそれをした方法は、最初は1つの変数が多すぎたというものでした。
people = user.downline(user, [], [])