簡體   English   中英

如何查詢NDB中的一對多關系?

[英]How to query an one-to-many relationship in ndb?

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中,這種方法會引發錯誤。 文檔並沒有說明這一點。

知道我將如何實現嗎? 謝謝

糊塗了一個多小時后,我終於偶然發現了這份備忘單 它很好地比較了經典db和ndb的功能。

根據備忘單,ndb不再支持collection_set的概念,這很可惜。 我發現它更加合乎邏輯。

現在,您必須執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM