Datastore Python Client Libraries: Query ndb.Polymodel

June 4, 2019

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

from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel

class 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 datastore

log = 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']}")
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.