TAGS :Viewed: 9 - Published at: a few seconds ago

[ How to retrieve object by entityKey with endpoints-proto-datastore? ]

I have a model

class MyModel(EndpointsModel):
    _message_fields_schema = ('entityKey', 'prop')
    prop = ndb.StringProperty()

and an API method:

@MyModel.method(request_fields=('entityKey',),
                  path='mymodel/{entityKey}', http_method='GET', name='mymodel.get')
def mymodel_get(self, mymodel):
    if not mymodel.from_datastore:
      raise endpoints.NotFoundException('mymodel not found.')
    return mymodel

But when I try a query like

http://localhost:8080/_ah/api/myapi/v1/mymodel/adsfasf-cm9jay1zdG9wchELEgRBcmVhGICAgICAgIAJDA

I get a 404. I know the object exists, and I know that's the correct urlsafe key. What's going on? The same code works when using 'id' instead of 'entityKey' and query with the integer key.

Answer 1


The entityKey you mention is the urlsafe combination of the object's type and id.

To create a key from that string use this code:

rev_key = ndb.Key(urlsafe=urlString)

For more information see this section in the NDB Datastore API docs.

Answer 2


The code you have written is correct, your model and method are fine. Which leads me to believe your problem is actually in the app.yaml file.

Make sure one of your handlers looks like this:

handlers:
- url: /_ah/spi/.*
  script: MyApi.APPLICATION

Note that although the handler points to /_ah/spi/.* the actual path is /_ah/api/

Good luck