Bolster
Bolster

Reputation: 7916

Querying for Unique Ancestors of models in Google App Engine DS

Anyone have any clean ways to get a unique list of the ancestors of entities of a particular model?

Eg If there is a class A, and a class B(parent=A), can one find all the A's that currently have B's

Upvotes: 1

Views: 106

Answers (2)

Bolster
Bolster

Reputation: 7916

Ended up doing a one liner;

a_with_b = set([b.parent() for b in B.all(keys_only=True)]) #list comprehension

FYI, in python 3, you could do

a_with_b = {b.parent() for b in B.all(keys_only=True)} #set comprehension

Upvotes: 2

Calvin
Calvin

Reputation: 4195

The simplest method I can think of is to do a keys_only query on B's and store their parent keys. If you have a lot of B's this will be a pain in the A. ;)

all_b = B.all(keys_only=True)

a_with_b = []

for b in all_b:
    parent = b.key().parent()

    if not parent:
        continue

    if parent not in a_with_b:
        a_with_b.append(parent)

Upvotes: 0

Related Questions