pnmtu13894 pnmtu13894 - 1 year ago 91
Python Question

Struggling with retrieving entities from datastore

I have some problems with Datastore in Google AppEngine.

I have 2 Models in the datastore which are:

class Product(ndb.Model):
name = StringProperty(required=True)
category = KeyProperty(kind=Category)
description = TextProperty(required=True)
price = IntegerProperty(required=True)


class Category(ndb.Model):

So how can i get all the datas from Product Class that belong to a certain Category?

In Relational SQL i used:

SELECT * FROM Product WHERE category = 'CertainName';

Thank you!

Answer Source

The following should perform what your sql query is doing:

query = Product.query(Product.category == "CertainName") # Note the "=="


I just realized you need to filter by key(thank you @mgilson). The query changes a little. Basically you just need to first get your category_entity and then apply the filter like this:

query = Product.query(Product.category == category_entity.key)

Refer to for more info (look at query_purchases_for_customer_via_key)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download