GAE db.ReferenceProperty 自引用
是否可以通过 db.ReferenceProperty 引用 self,是否可以构造一个列表?
例如,我有一个 User 类,我想要为他/她的朋友提供一个引用 User 类的属性?
我目前正在使用Python。
编辑:
所以如果我有这个类:
class Node(db.Model):
name = db.StringProperty(required=True)
neighbour = db.SelfReferenceProperty(collection_name="neighbours")
node1 = Node(name="node1")
node2 = Node(name="node2")
node1.neighbour = node2
node2.neighbour = node1
node1.put()
node2.put()
我可以通过 node1.neighbours
和 node2.neighbours
访问其他节点的列表吗?
或者我应该这样做:
class Node(db.Model):
name = db.StringProperty(required=True)
neighbour = db.ListProperty(Node)
node1 = Node(name="node1")
node2 = Node(name="node2")
node1.node = [node2]
node2.neighbour = [node1]
node1.put()
node2.put()
另外,我如何能够添加/删除/修改上面两个示例中的邻居列表?
谢谢
Is it possible to reference self via the db.ReferenceProperty, and is it possible to construct a list?
For example I have a class for User and I want a property for his/her friends that references the User class?
I'm currently using Python.
Edit:
So if I have this class:
class Node(db.Model):
name = db.StringProperty(required=True)
neighbour = db.SelfReferenceProperty(collection_name="neighbours")
node1 = Node(name="node1")
node2 = Node(name="node2")
node1.neighbour = node2
node2.neighbour = node1
node1.put()
node2.put()
can I access a list of other nodes via node1.neighbours
and node2.neighbours
?
Or should I do this:
class Node(db.Model):
name = db.StringProperty(required=True)
neighbour = db.ListProperty(Node)
node1 = Node(name="node1")
node2 = Node(name="node2")
node1.node = [node2]
node2.neighbour = [node1]
node1.put()
node2.put()
Also how would I be able to add/delete/modify the list of neighbours in both examples above?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是。
Yes.
另请参阅此相关 SO 问题:SelfReferenceProperty 与 ListProperty Google App Engine
See also this related SO question: SelfReferenceProperty vs. ListProperty Google App Engine