如何在GAE Python中创建子实体和父实体?

2024-04-27 19:45:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我的应用程序中有以下数据库模型:

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

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

我怎样才能告诉程序“许多帖子属于一个帐户”?你知道吗

谢谢!你知道吗


Tags: text模型数据库应用程序modelemailusernameaccount
1条回答
网友
1楼 · 发布于 2024-04-27 19:45:31

我认为有两种方法。一个简单的方法是在Post模型上添加另一个字段,将其绑定到帐户。e、 g.假设所有帐户都有唯一的userid

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

现在,如果您有一个帐户,您可以通过查询userid与帐户相同的位置来获取它的“子级”。你知道吗

第二种方法是通过使用"ancestor paths"来构造数据的“强一致性”。然后您可以通过执行"ancestor query"来获得帖子。你知道吗

相关问题 更多 >