Let's look at it systematically. Firstly, the db-> ndb translation guide tells us that a query returning SubCategory instances for a given instance of a category looks like this:
subcats_query = SubCategory.query(SubCategory.category == cat.key)
(Note that I use lowercase names for instances / entities and CapWords for classes / models. Thus, cat is a category entity.)
So, in Python, we will write your double loop as follows:
for cat in categories: for subcat in SubCat.query(SubCat.category == cat.key): ...blah...
To turn this into something easily accessible from the template, let's define a new method in the Category class:
class Category(ndb.Model): supercat = ndb.KeyProperty(kind=SuperCat) def subcat_set(self): return SubCat.query(SubCat.category == self.key)
Note that this is a method; in Python you should call it:
for cat in categories: for subcat in cat.subcat_set(): ...blah...
But in the template, you can (should) leave the call syntax () to get what you want:
{{ for cat in categories }} {{ for subcat in cat.subcat_set }} ...blah...
Give it a try!
source share