python - GAE : Can't use imported class with Endpoints API method

标签 python google-app-engine google-cloud-endpoints google-app-engine-python

我有一个类,其中包含写入我的数据存储的方法。我可以使用它从我的网络写入我的数据存储,但我不知道如何将它用于我的 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/

相关文章:

java - 奇怪的 Java 异常行为

java - 部署时出现 GAE 端点错误 500

python - tf.train.shuffle_batch 和 `tf.train.batch 发生了什么?

python - 使用 Selenium Python 客户端在不同浏览器中处理 headless (headless)模式

google-app-engine - Google App Engine - 获取符合条件超过 1000 条的记录数

java - Google Cloud Endpoints 设置问题

python - 从 PuLP 到 GEKKO : Syntax Mapping for Constraints, 目标函数

python - 使用 TensorFlow 移动梯度

database - 如何在 Google App Engine 中创建数据库表

java - GAE+JDO : Deleting Child object cause issue in google app engine java