2014-04-12 89 views
1

我在我的應用程序下面的數據庫模型:如何在GAE Python中創建子實體和父實體?

class Account(ndb.Model): 
    username = ndb.StringProperty() 
    userid = ndb.IntegerProperty() 
    email = ndb.StringProperty() 

class Post(nbd.Model): 
    text = nbd.StringProperty() 

我怎麼可以告訴大家,「很多帖子都屬於一個帳戶」的方案?

謝謝!

回答

1

我覺得有幾種方法。一個簡單的方法是在Post模型中添加另一個字段,將其與帳戶綁定。例如假設所有賬戶都有一個獨特的userid

def Post(ndb.Model): 
    text = ndb.StringProperty() 
    userid = ndb.IntegerProperty() 

現在,如果你有一個帳戶,你可以得到它的「孩子們」通過查詢,其中用戶ID是相同的帳戶。

第二種方法是使用"ancestor paths"來構建「強一致性」數據。然後,您可以通過執行"ancestor query"來獲得帖子。

相關問題