【问题标题】:Comparing a key to a KeyProperty in ndb将密钥与 ndb 中的 KeyProperty 进行比较
【发布时间】:2013-02-08 20:23:37
【问题描述】:

我正在尝试进行比较键的查询,但没有得到任何结果。

我有一个教程和一个章节模型。章节模型包含一个 keyProperty 类型的 Tutorial。 (tutKey = ndb.KeyProperty(kind='Tutorial'))

tutID = self.request.cookies.get('tut_id', '')

tutIns = ndb.Key('Tutorial', int(tutID)).get()
chaps = Chapter.query(Chapter.tutKey == tutIns.key)

self.render('editTut.html', chaps=chaps, tutins=tutIns.key)

我发送tutIns.key 只是为了测试实例是否正常工作,是的,它返回密钥(Key('Tutorial', 1))。此外,如果我只查询 Chapter.query() 它会按预期返回所有章节。

这就是我在章节中存储 tutkey 的方式:

tutID = self.request.cookies.get('tut_id', '')
tutorial = ndb.Key('Tutorial', tutID)
.
.
.

chap = Chapter(tutKey=tutorial, title=title, content=content,
                           note=note, order=order)
chap.put()

在开发控制台中,我可以看到存储在第 1 章和第 2 章中的 tutkey 是相同的密钥,但该密钥不等于教程密钥。我是否以错误的方式创建章节?

【问题讨论】:

    标签: python google-app-engine app-engine-ndb


    【解决方案1】:

    在这里,您将 ID 转换为 int:

    tutIns = ndb.Key('Tutorial', int(tutID)).get()
    

    但是在这里,您将其用作字符串:

    tutID = self.request.cookies.get('tut_id', '')
    tutorial = ndb.Key('Tutorial', tutID)
    

    生成的 Key 实例不相等。

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多