waigani
waigani

Reputation: 3580

Python model with a list of self references

I would like to do something like this:

class Author(db.Model):
    influencedBy = db.ListProperty(db.SelfReferenceProperty())
    influenced = db.ListProperty(db.SelfReferenceProperty())

I know I can use self.author_set to get the reverse relationship, but I need to add multiple Author objects to influenced and influencedBy.

Upvotes: 1

Views: 112

Answers (1)

Tim Hoffman
Tim Hoffman

Reputation: 12986

Unfortunately you can't do what you propose. As you probably have realized you will get ValueError: Item type ReferenceProperty is not acceptable

You could do influencedBy = db.ListProperty(db.Key) or if you used ndb influencedBy = ndb.KeyProperty(kind=Author,repeated=True)

Once thing to consider is how many members might be in influencedBy and influenced. If its possible for there to be large numbers then you might run into entity size issues. If that is a possibility you then need to consider using a separate entity to record influence.

Upvotes: 1

Related Questions