我想让我的 GAE 后端 API 返回一个联系人列表以及每个联系人的相应电子邮件集合。我正在使用endpoints-proto-datastore并按照此问题中的指南实施此操作。
我的问题是,当contacts_list
调用该方法时,我得到:
Encountered unexpected error from ProtoRPC method implementation: BadValueError (Expected Key, got [])
我猜这是因为 Contact.email_keys 可能是空的 ([]) 但不知道在哪里控制这种行为。
这是我的 API 实现的相关部分:
class Email(EndpointsModel):
type = ndb.StringProperty(choices=('home', 'work', 'other'))
email = ndb.StringProperty()
#.... other properties
class Contact(EndpointsModel):
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
email_keys = ndb.KeyProperty(kind=Email, repeated=True)
#.... other properties
@EndpointsAliasProperty(repeated=True, property_type=Email.ProtoModel())
def emails(self):
return ndb.get_multi(self.email_keys)
_message_fields_schema = ('id', 'first_name', 'last_name')
@endpoints.api(name='cd', version='v1')
class MyApi(remote.Service):
@Contact.query_method(query_fields=('limit', 'order', 'pageToken'),
collection_fields=('id', 'first_name', 'last_name', 'emails'),
path='contacts', name='contacts.list')
def contacts_list(self, query):
return query