python - Google App Engine - Multiple child/parent -
i want make friend system have db model this:
class users(ndb.model): username = ndb.stringproperty(required = true) bio = ndb.stringproperty() class friend_list(ndb.model): list = ndb.stringproperty(repeated=true) class friend_pending(ndb.model): list = ndb.stringproperty(repeated=true)
friend_pending model friend not yet accepted. while friend_list model friend accepted.
i want make both friend_list , friend_pending child of users entity. possible?
here's second approach if not possible:
class users(ndb.model): username = ndb.stringproperty(required = true) bio = ndb.stringproperty() class friend_list(ndb.model): user_username = ndb.stringproperty(required = true) list = ndb.stringproperty(repeated=true) class friend_pending(ndb.model): user_username = ndb.stringproperty(required = true) list = ndb.stringproperty(repeated=true)
if both possible, better cost , performance?
i want make both friend_list , friend_pending child of users entity. possible?
yes. when create entity, can use "parent" parameter designate parent (or parents) entity.
google's entity keys section covers 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 friend list , set parent user create above friend_list = friend_list(parent=user) #save datastore friend_list.put()
keep in mind users class in gae specially defined , has additional functions need acknowledge. see documentation here.
if both possible, better cost , performance?
i can't sure because don't know how using these models, in most(maybe all) cases first approach more efficient.
lastly, correct naming convention datastore models capitalize first letter. example, friend list class should "friend_list".
Comments
Post a Comment