开发者

GAE db.ReferenceProperty Self referencing

开发者 https://www.devze.com 2023-02-03 01:25 出处:网络
Is it possible to reference self via the db.ReferenceProperty, and is it possible to construct a list?

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开发者_如何学Python
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


Yes.

class SelfReferenceProperty(verbose_name=None, collection_name=None, ...)

A reference to another model instance of the same class. See ReferenceProperty.

Value type: db.Key (see above)


See also this related SO question: SelfReferenceProperty vs. ListProperty Google App Engine

0

精彩评论

暂无评论...
验证码 换一张
取 消