python - Many-To-Many Relationship in ndb -
python - Many-To-Many Relationship in ndb -
trying model many-to-many relationship ndb. can point illustration of how this?
at here illustration of have @ moment:
class person(ndb.model): guilds = ndb.keyproperty(kind="guild", repeated=true) class guild(ndb.model) members = ndb.keyproperty(kind="person", repeated=true) def add_person(self, person): self.members.append(person.key) self.put() person.guilds.append(self.key) person.put()
is right way go it? have had around can't seem find documentation on subject.
in datastore viewer, can see relationship beingness stored list of keys, expect.
however, when seek utilize them in person class methods this:
for guild in self.guilds:
i get:
typeerror: 'keyproperty' object not iterable
no. not right way go it.
you can model many-to-many relationship 1 repeated property:
class person(ndb.model): guilds = ndb.keyproperty(kind="guild", repeated=true) class guild(ndb.model): @property def members(self): homecoming person.query().filter(person.guilds == self.key) def add_person(self, person): person.guilds.append(self.key) person.put()
or viceversa with:
class person(ndb.model): @property def guilds(self): homecoming guild.query().filter(guild.members == self.key) class guild(ndb.model): members = ndb.keyproperty(kind="person", repeated=true) def add_person(self, person): self.members.append(person.key) self.put()
the direction of relationship depends on many factors (your business model, number of guilds per person, number of members per guild, etc.)
python google-app-engine many-to-many app-engine-ndb
Comments
Post a Comment