2

我有一個模型如何通過entityKey和endpoints-proto-datastore檢索對象?

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

和API方法:

@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 

但是,當我嘗試像

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

查詢,我收到了404我知道對象存在,我知道這是正確的urlsafe鍵。這是怎麼回事?使用'id'而不是'entityKey'並使用整數鍵查詢時,相同的代碼有效。

回答

0

entityKey你提到是的urlsafe組合對象的typeid

要創建從該字符串使用密鑰驗證碼:

rev_key = ndb.Key(urlsafe=urlString)

欲瞭解更多信息,請參閱this section in the NDB Datastore API docs

0

你寫的代碼是正確的,你的模型和方法都很好。 這使我相信你的問題實際上是在app.yaml文件中。

確保您的處理程序之一是這樣的:

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

注意的是,雖然處理程序指向/_ah/spi/.*實際路徑是/_ah/api/

好運