我有一个类,其中包含写入我的数据存储的方法。我可以使用它从我的网络写入我的数据存储,但我不知道如何将它用于我的 API POST。
数据存储写入.py:
from endpoints_proto_datastore.ndb import EndpointsModel
from google.appengine.ext import ndb
def person_timestamp_key(person_timestamp):
return ndb.Key('Person Timestamp', person_timestamp)
class Person(EndpointsModel):
name = ndb.StringProperty(indexed=False)
timestamp = ndb.DateTimeProperty(auto_now_add=True)
def do_insert(self, name):
person_timestamp = 'person_timestamp'
# Sets entity
person = Person(parent=person_timestamp_key(person_timestamp))
person.name = name
person.put()
webapp.py - 当从 web 应用程序提交时,这会成功地将一个条目添加到我的数据存储中:
def post(self):
name = self.request.get('name')
person = DatastoreWrite.Person()
if name and not name.isspace():
person.do_insert(name)
self.redirect('/')
api.py - 我在这里尝试了一百种不同的方法。这就是我目前所拥有的。结果为“BadValueError:预期字符串,得到 Person()”:
class Api(remote.Service):
@Person.method(path='api', http_method='POST', name='person.insert')
def PersonInsert(self, person):
person = DatastoreWrite.Person()
person.do_insert(person)
return person
更新:另一次尝试 - 失败并出现错误“BadValueError:预期字符串,得到 Person(name=u'Atlas')”:
@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
def PersonInsert(self, name):
person = DatastoreWrite.Person()
person.do_insert(name)
return person
最佳答案
do_insert函数有一个签名def do_insert(self, name)
它需要 name 作为字符串参数,并且您将 person 作为参数发送 person.do_insert(person)
.
解决方案:
@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
def PersonInsert(self, person):
person.do_insert(person.name)
return person
关于python - GAE : Can't use imported class with Endpoints API method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232937/