views:

45

answers:

1

I am trying to use google appengine. I have this model:

def Human(db.Model):
 name = db.StringProperty()
 friends = db.SelfReferenceProperty()

This Human has more than one friend. So, how to handle this with google appengine?

+4  A: 

For simple many-to-many relationships, use a ListProperty with a list of keys.

If you need to store additional metadata, give the model its own relationship, e.g. Friendship.

Examples of both can be found @ http://code.google.com/appengine/articles/modeling.html

Drew Sears
Can you show me an example to store additional metadata? e.g. Friendship
ndemir
Look for ContactCompany on the link above. It's in the last example, and it would be the equivalent of your Friendship model.
Drew Sears
One note regarding their example: they model a many-to-many relationship between two different models. Since you're just using one model, instead of contact and company you would have something like friend1 and friend2. You'd probably want some logic to determine consistent positioning, e.g. if "Bob" and "Mary" are friends, Bob is always friend1 because he is first alphabetically. You'd also have to do 2 queries to resolve Bob's friendships, checking for either entities where he is friend1 or friend2.
Drew Sears