python - 我无法通过查询获取实体

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

我使用的过程与我之前的项目中使用的过程相同,这是我从 Google 提供的教程中学到的,但这次我没有得到任何结果。

我的代码是从我的数据存储中获取 3 个随机笑话,该数据存储仅保留 100 个笑话并在 HTML 表格中显示它们。

这是我的模型:

class joke(db.Model):
jokeID = db.IntegerProperty()
content = db.TextProperty()

这是我的 Controller 中获取实体的代码:

def get(self):
deck = range(1, 101)
shuffle(deck)
items = list()
itemCounter = 0
for jokeNumber in deck:
    itemCounter += 1
    if itemCounter <= 3:
        self.response.out.write(jokeNumber)
        # I tried with fetching from the model & with GqlQuery
        #items.append(joke.all().filter('jokeID=',jokeNumber).fetch(1))
        items.append(db.GqlQuery("SELECT * FROM joke WHERE jokeID=:1",jokeNumber))
    else:
        break
template_values = {'items' : items}
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path,template_values))

& 这里是我用 Controller 中的数据填充 HTML 表格的地方:

<table border="0" width="800px">
     {% for item in items %}
      <form method="post" action="/ocena">
          <tr>
            <td>
                {{ item.content }}
            </td>
          </tr> 
       </form>
     {% endfor %}
   </table>

在网站的源代码中,我得到了三个空单元格,但是当我在 GAE 的数据存储查看器中执行查询时,我得到了所需的结果。

最佳答案

我检查了你的示例,这段代码对我有用:

def get(self):
    deck = range(1, 101)
    shuffle(deck)
    items = db.GqlQuery("SELECT * FROM joke WHERE jokeID IN :1", deck[:3])
    template_values = {'items' : items}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path,template_values))

关于python - 我无法通过查询获取实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7425243/

相关文章:

python - Pandas 放大多维物体

Python 正则表达式不给我任何匹配项

android - 尝试在 Android Studio 上运行 Enhance

python - 为什么 GQL 查询不匹配?

python - 如何访问数据存储信息(交互式控制台)?

python - DateTimeProperty 在 Google App Engine 中设置为日期时间时出错

python - reshape 数据帧以与另一个数据帧具有相同的索引

python - 12296 :26672:0420/163936. 459 :ERROR:browser_switcher_service. cc(238) XXX Init() "Selenium Python"错误

python - 如何从 csv 文件将 6000 条记录上传到 Google Datastore

java - google appengine mapper - 映射日期范围