2014-02-16 42 views
1

tl; dr是否有可能通過端點 - 原型數據存儲從POST接收包含對象的列表並將其插入db?POST對象列表帶有端點 - 原型數據存儲

下面的示例,當我建立我的API我沒有得到我怎麼可以讓用戶發佈的對象列表,以便我可以更有效地將一堆數據放在db使用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) 

- 編輯 -

回答

3

docsPOST荷蘭國際集團到數據存儲之外,唯一的問題是,你的模型是不是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 
+0

感謝您的回答,但我想你誤會了。這就是它使用端點文檔給出的例子解決問題的方式。其實我沒有一個帶有'ndb.StructuredProperty'的模型,其中包含另一個EndpointsModel。 剛編輯的問題。 –

+0

哦,對不起,現在我明白了。它明確地解決了這個問題。謝謝! –

相關問題