Group by parent in NDB request

Let's say I have a model with school, cool, student models. In this model, classes are children of the school, and students are children of the class. I would like to get all the students grouped by their class.

Using NDB, I can get call students for school A with:

school_key = ndb.Key( 'School', 'A' )
all_students_in_A = Student.query( ancestor = school_key ).fetch( )

However, I would like to get students in groups A by class. Looking at the document, the request accepts the group_by argument, but only for real model fields.

+4
source share
2 answers

I am using the python groupby function from itertools to publish processes and group objects.

, , .

.

http://docs.python.org/2/library/itertools.html#itertools.groupby

, , , , , / , , . , - .

+2

Python .

classes = defaultdict(list)
for student in all_students_in_A:
    class = student.key.id()
    classes[class].append(student)

- dict, - , - .

+1

All Articles