【问题标题】:Google App Engine NDB Datastore get_by_id to get entityGoogle App Engine NDB Datastore get_by_id 获取实体
【发布时间】:2013-12-27 06:37:23
【问题描述】:

我正在尝试使get_by_id() 工作,但我不确定我在做什么是错误的,或者它真的不起作用。我希望它返回一个类似于ndb.Key('Organization',id).get() 的实体,但两者都不起作用。这是我尝试过的:

class CompaniesPage(BaseHandler):
  def get(self):
    id = self.request.get('id')
    organizations = Organization.query()
    company = Organization.get_by_id(id) # first try
    values = {
      'organizations' : organizations,
      'company' : company,
      'id' : id
    }
    self.render_html('companies.html',values)

第二次尝试:

class CompaniesPage(BaseHandler):
  def get(self):
    id = self.request.get('id')
    organizations = Organization.query()
    company = ndb.Key('Organization',id).get() # second try
    values = {
      'organizations' : organizations,
      'company' : company,
      'id' : id
    }
    self.render_html('companies.html',values)

这是我使用Jinja2 的html。

 <h3> {{ company }} <small>{{ id }} Category</small></h3>

我收到以下文字:

无 6201245580656640 类别

我尝试遵循 Google App Engine 文档中关于 Model get_by_id 的参考。

【问题讨论】:

    标签: google-app-engine google-cloud-datastore jinja2 app-engine-ndb


    【解决方案1】:

    您还没有说您是如何创建实体的,但是根据您的示例None 6201245580656640 Category6201245580656640 这样的 id 告诉我您可能正在使用自动生成的 id,它是一个整数而不是字符串。您正在从请求对象中获取 id,这将是一个字符串。

    在您的通话中将其转换为 int get_by_id(int(id))

    【讨论】:

    • 是的。这是一个自动生成的 ID。我没有使用键名,而是使用了 id。我也试过str(id),但不是int(id)。我又试了一次,现在似乎可以正常工作了。
    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2013-01-27
    • 2017-04-13
    • 1970-01-01
    • 2015-07-25
    • 2015-07-25
    相关资源
    最近更新 更多