POST端点对象w / endpoints-proto-datastore

时间:2014-02-16 18:16:23

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

tl; dr 是否可以使用endpoints-proto-datastore从POST接收包含对象的列表并将其插入db?

在示例之后,在构建我的API时,我没有得到如何让用户POST一个对象列表,以便我可以更高效地使用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)

- 编辑 -

1 个答案:

答案 0 :(得分:3)

请参阅有关POST数据存储的docs,您唯一的问题是您的模型不是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
相关问题