Google App Engine Datastore (ndb) 中的多对多关系 [英] Many-To-Many Relationships in Google App Engine Datastore (ndb)

查看:22
本文介绍了Google App Engine Datastore (ndb) 中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:成员和事件.一个会员可以参加很多活动,在一个活动中,有很多参与者.我是这样想的:

I have two models: Members and Events. A member can participe in many events and in an event, there are many participants. I think about like this:

class Members(ndb.model):
    event = ndb.KeyProperty(repeated=True)

class Events(ndb.model):
    member = ndb.KeyProperty(repeated=True)

处理多对多关系的最佳方法是什么?

What's the best way to do many-to-many relationship?

推荐答案

我认为在这种情况下,您希望一个模型中的键数组/列表指向另一个模型.由于数组/列表的长度有限制(现在最大为 5000),您可能希望成员模型指向事件模型(我假设成员可能不会参加很多活动(超过5000),但在一个活动中有很多(超过 5000)名成员).现在,如果您想要参与者列表,您只需在成员模型中查询事件键即可.如果你只想要人数,你可以做一个key only查询,这样可以节省成本.

I think in this case you want to have an array/list of keys in one model pointing to the other model. Since there is a limit on the length of the array/list (the max is 5000 right now) you probably want to have the member model point to the events models (I am assuming members probably don't go to many events (more than 5000), but there are many (more than 5000) members at an event). Now if you want the list of attendees you just query for the event key in the members models. You can do a key only query if you just want the head count, this will save on cost.

如果由于某种原因您需要使两个案例都超过 5000,那么您可以制作另一个(参加者)模型,其中包含一个成员密钥和一个事件密钥,然后为每个事件的每个来宾制作此参加者模型.要获取来宾列表,只需查询此模型的特定事件,如果您需要成员的事件列表,请执行相反的操作.这并不比上述工作多多少,因此即使 5000 不是限制因素,这也可能是您的首选方式.

If for some reason you need to have both cases be over 5000, then you can make another (attendee) model that contains a member key and an event key, then make this attendee model for each guest for each event. To get the list of guests just query this model for a specific event, if you need a list of events for a member do the opposite. This isn't much more work than the above so might be your go to way of doing it even if 5000 isn't a limiting factor.

这篇关于Google App Engine Datastore (ndb) 中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆