Skip to content Skip to sidebar Skip to footer

Google App Engine - Multiple Child/parent

I want to make friend system that have db model like this: class Users(ndb.Model): username = ndb.StringProperty(required = True) bio = ndb.StringProperty() class friend_l

Solution 1:

I want to make both friend_list and friend_pending to be child of Users entity. Is it possible?

Yes. When you create an entity, you can use the "parent" parameter to designate a parent (or parents) for the entity.

Google's Entity Keys section covers this well.

Example:

#Create User entity
#This code assumes you're using GAE's built-in user's class
user = users.User("Albert.Johnson@example.com")
user.put()

#Create a friend list and set its parent to the user we create above
friend_list = Friend_List(parent=user)

#Save to datastore
friend_list.put()​

Keep in mind that the Users class in GAE is specially defined and has additional functions that you need to acknowledge. See the documentation here.

If both are possible, which are better for cost and performance?

I can't say for sure because I don't know exactly how you will be using these models, but in most(maybe all) cases your first approach would be more efficient.

Lastly, the correct naming convention for Datastore models is to capitalize the first letter. For example, your friend list class should be "Friend_List".


Post a Comment for "Google App Engine - Multiple Child/parent"