tl;dr:是否可以使用端点原型(prototype)数据存储从 POST 接收包含对象的列表并将其插入数据库? p>
按照示例,在构建我的 API 时,我不知道如何让用户发布对象列表,以便我可以更有效地使用 ndb.put_multi 将一堆数据放入数据库中
,例如。
来自此评论:endpoints_proto_datastore.ndb.model我认为它的设计方式是不可能的。我是对的还是我错过了什么?
扩展the sample provided by endpoints达到预期目的:
class Greeting(messages.Message):
message = messages.StringField(1)
class GreetingCollection(messages.Message):
items = messages.MessageField(Greeting, 1, repeated=True)
# then inside the endpoints.api class
@endpoints.method(GreetingCollection, GreetingCollection,
path='hellogretting', http_method='POST',
name='greetings.postGreeting')
def greetings_post(self, request):
result = [item for item in request.items]
return GreetingCollection(items=result)
--编辑--
最佳答案
请参阅docs关于 POST
到数据存储区,您唯一的问题是您的模型不是 EndpointsModel
。相反,为您的 Greeting
和 GreetingCollection
定义一个数据存储模型:
from endpoints_proto_datastore.ndb import EndpointsModel
class Greeting(EndpointsModel):
message = ndb.StringProperty()
class GreetingCollection(EndpointsModel):
items = ndb.StructuredProperty(Greeting, repeated=True)
完成此操作后,您可以使用
class MyApi(remote.Service):
# ...
@GreetingCollection.method(path='hellogretting', http_method='POST',
name='greetings.postGreeting')
def greetings_post(self, my_collection):
ndb.put_multi(my_collection.items)
return my_collection
关于python - 带端点原型(prototype)数据存储的 POST 对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21815401/