Datastore Python Client Libraries: Query ndb.Polymodel

Jun 4, 2019

Assuming you was using App Engine Python 2.7 with ndb.PolyModel.

from google.appengine.ext import ndbfrom google.appengine.ext.ndb import polymodelclass Item(polymodel.PolyModel):  name = ndb.StringProperty()  is_active = ndb.BooleanProperty()class Country(Item):  population = ndb.IntegerProperty()

Query using Datastore Python Client Libraries.

from google.cloud import datastorelog = logging.getLogger(__name__)def init_datastore():    return datastore.Client()    # return datastore.Client.from_service_account_json('keys/PROJECT_ID-datastore.json')db = init_datastore()query = db.query(kind='Item')query.add_filter('class', '=', 'Country')query.add_filter('is_active', '=', True)items = query.fetch()for _item in items:    log.info(f"name={_item['name']}")

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.