Ivaylo Toskov Ivaylo Toskov - 2 months ago 15
Python Question

How to access Google Cloud Datastore entity with dash in its name?

I am working on a Google App Engine project and I need to access a Datastore entity with a name that contains dash, e.g.

random-entity
. I want to do that in Python. Since
random-entity
is invalid syntax for a class name, I cannot create a model and access it like that.

So how can I access this entity? Is it possible to do that without creating a model and just retrieve it in JSON format?

Keep in mind that renaming the entity is not an option for the project I am working on.

Answer

If you are using NDB library you need to override class method _get_kind(cls) of model, like this:

class RandomEntity(ndb.Model):

  @classmethod
  def _get_kind(cls)
    return 'random-entity'

  # You can override property name as well
  random_name = ndb.StringProperty('random-name')