【问题标题】:How to query an one-to-many relationship in ndb?如何在ndb中查询一对多关系?
【发布时间】:2013-06-20 09:18:54
【问题描述】:
class User(UserMixin, ndb.Model):
    email = ndb.StringProperty(required = True)
    password_hash = ndb.TextProperty(required = True)

class Record(ndb.Model):
    user = ndb.KeyProperty(kind=User)
    notes = ndb.TextProperty()

在 Django 和我相信经典的 GAE-db 中,您会像这样查询属于用户的记录:

user = "get user instance"
user.record_set.all()

但在 ndb 中,这种方法会引发错误。 Documentation 并没有说明这一点。

知道我将如何实现这一目标吗?谢谢

【问题讨论】:

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


    【解决方案1】:

    在谷歌上搜索了一个多小时后,我终于偶然发现了这个cheatsheet。它很好地比较了经典 db 和 ndb 的特性。

    根据备忘单,ndb 不再支持collection_set 的概念,这很遗憾。我发现它更符合逻辑。

    现在你必须这样做:

    records = Record.query(Record.user == user.key)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2013-11-22
      • 2011-11-13
      • 1970-01-01
      • 2013-08-28
      相关资源
      最近更新 更多