Google App Engine - 多个孩子/父母 [英] Google App Engine - Multiple child/parent

查看:93
本文介绍了Google App Engine - 多个孩子/父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class用户(ndb.Model):
username = ndb.StringProperty(required = True)
bio = ndb.StringProperty()
$ b $ class friend_list(ndb.Model):
list = ndb.StringProperty(重复= True)

class friend_pending(ndb.Model):
list = ndb.StringProperty(repeated = True)

friend_pending是尚未接受的朋友模型。虽然friend_list是接受的朋友模型。



我想让friend_list和friend_pending都是Users实体的子代。是否有可能?



如果不可能,以下是第二种方法:

 <$ c $ ndb.StringProperty()
$ b $ class昵称(ndb.Model):
username = ndb.StringProperty(required = True)
):
user_username = ndb.StringProperty(required = True)
list = ndb.StringProperty(repeated = True)
$ b $ class friend_pending(ndb.Model):
user_username = ndb.StringProperty(required = True)
list = ndb.StringProperty(repeated = True)

如果两者都有可能,哪个更符合成本和性能? 我想要使friend_list和friend_pending都成为Users实体的子项。是否有可能?


是的。当你创建一个实体时,你可以使用parent参数来为该实体指定一个或多个父代。



Google的实体键部分涵盖了这一点。



示例:

 #创建用户实体
#此代码假设您使用的是GAE的内置用户类
user = users.User(Albert.Johnson@example.com)
user.put()

#创建一个好友列表并将其父对象设置为我们创建的用户在
以上friend_list = Friend_List(parent = user)

#保存到数据存储
friend_list.put()

请记住,GAE中的Users类是特别定义的,并且具有您需要确认的其他功能。请参阅此处的文档。


如果两种方法都可行,哪种方法更符合成本和性能?

我不能肯定地说,因为我不知道你将如何使用这些模型,但在大多数情况下(也许是所有情况),你的第一种方法会更有效率。



最后,Datastore模型的正确命名约定是首字母大写。例如,你的朋友列表班应该是Friend_List。


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_list(ndb.Model):
    list = ndb.StringProperty(repeated=True)

class friend_pending(ndb.Model):
    list = ndb.StringProperty(repeated=True)

friend_pending is model for friend that not yet accepted. While friend_list is model for friend that are accepted.

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

Here's the second approach if it is 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 are possible, which are better for cost and performance?

解决方案

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".

这篇关于Google App Engine - 多个孩子/父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆