Python GAE:Can';t将导入的类与端点API方法一起使用
我有一个类,其中包含一个写入数据存储的方法。我可以用它从我的web上写入数据存储,但我不知道如何在我的API文章中使用它 DatastoreWrite.py:Python GAE:Can';t将导入的类与端点API方法一起使用,python,google-app-engine,google-cloud-endpoints,google-app-engine-python,Python,Google App Engine,Google Cloud Endpoints,Google App Engine Python,我有一个类,其中包含一个写入数据存储的方法。我可以用它从我的web上写入数据存储,但我不知道如何在我的API文章中使用它 DatastoreWrite.py: from endpoints_proto_datastore.ndb import EndpointsModel from google.appengine.ext import ndb def person_timestamp_key(person_timestamp): return ndb.Key('Person Timestam
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-从webapp提交时,此操作将成功将条目添加到我的数据存储中:
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-我在这里尝试了100种不同的方法。这就是我目前拥有的。结果为“BadValueError:应为字符串,Get Person()”:
更新:另一次尝试-失败,错误为“BadValueError:Expected string,Get Person(name=u'Atlas')”:
do_insert函数有一个签名
def do_insert(self,name)
,该签名要求name作为字符串参数,并且您将person作为参数person发送。do_insert(person)
解决方案:
谢谢你解释这个错误。我再次尝试输入名称(上面的更新),但它也不起作用。您能否就如何在此处正确调用do_insert方法提出建议?在endpoints_proto_数据存储中,传入的请求消息始终是其方法所在的模型类,在您的情况下,它是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
@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
def PersonInsert(self, name):
person = DatastoreWrite.Person()
person.do_insert(name)
return person
@DatastoreWrite.Person.method(path='healthsecure', http_method='POST', name='person.insert')
def PersonInsert(self, person):
person.do_insert(person.name)
return person