python - 带端点原型(prototype)数据存储的 POST 对象列表

标签 python google-app-engine endpoints-proto-datastore

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。相反,为您的 GreetingGreetingCollection 定义一个数据存储模型:

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/

相关文章:

python - 为什么 MlabSceneModel 的 light_manager 没有设置?

python - 使用 python HL7 解析多条消息

使用translate()方法的Python TypeError

node.js - 在 Google Cloud App Engine 上运行 GraphQL 服务器

python - 谷歌应用程序引擎 - NDB - KEY

google-app-engine - Google App Engine 突然开始端点身份验证失败

python - 有没有办法保护谷歌云端点原型(prototype)数据存储?

python - 垂直对齐 str 到相反的水平序列

google-app-engine - 使用App Engine插件遵循Grails脚手架教程时,list.gsp FileNotFoundException