我正在尝试做类似以下的事情。基本上_message_fields_schema
设置为有限的属性,不需要获取另一个RPC调用(例如,引用其他对象的键属性)。这样可以避免加载所有数据,从而加快查询速度。并且当我希望它们与API一起返回时,允许我request_fields=MyModel.get_fields
来获取这些内容。
但是,对于具有MyModel的键引用的对象,我认为类似下面的内容应该这样做,但我看到验证错误,如下面的代码所示。不知道这是一个错误还是我可能做错了什么?
class MyModel(EndpointsModel):
# More expanded fields.
get_fields = (...)
# Default fields.
_message_fields_schema = (...)
class MyOtherModel(EndpointsModel):
my_obj_key = ndb.KeyProperty(MyModel)
@EndpointsAliasProperty(
property_type=MyModel.ProtoModel(fields=MyModel.get_fields),
setter=MyObjSet)
def my_obj(self):
return self.my_obj_key.get()
错误堆栈跟踪。
Encountered unexpected error from ProtoRPC method implementation: ValidationError (Expected type <class '.MyModelProto_property1_property2_property3_property4'> for field myrecord, found <MyModel
property1: 123
property2: 'value2'
property3: u'value3'
property4: 'value4'> (type <class '.MyModel'>))
Traceback (most recent call last):
File "google_appengine/lib/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
response = method(instance, request)
File "google_appengine/lib/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
return remote_method(service_instance, request)
File "google_appengine/lib/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
response = method(service_instance, request)
File "third_party/py/endpoints_proto_datastore/ndb/model.py", line 1597, in QueryFromRequestMethod
next_cursor=next_cursor)
File "third_party/py/endpoints_proto_datastore/ndb/model.py", line 1275, in ToMessageCollection
for item in items]
File "third_party/py/endpoints_proto_datastore/ndb/model.py", line 1171, in ToMessage
return proto_model(**proto_args)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 747, in __init__
setattr(self, name, value)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 926, in __setattr__
object.__setattr__(self, name, value)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1559, in __set__
super(MessageField, self).__set__(message_instance, value)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1232, in __set__
self.validate(value)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1321, in validate
self.__validate(value, self.validate_element)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1287, in __validate
validate_element(value)
File "google_appengine/lib/protorpc-1.0/protorpc/messages.py", line 1272, in validate_element
(self.type, name, value, type(value)))