开发者

GAE python - how to change the "one" that a "many" object points to?

I'm using the GAE database to store objects of type Supp, which are 开发者_如何学运维part of a SuppSet. A SuppSet can have many Supps in it. I'm using the ReferenceProperty model to create the one-to-many relationship between the SuppSet and Supps as follows:

class SuppSet(db.Model):
    <stuff>

class Supp(db.Model):
    <more stuff>
    suppset    = db.ReferenceProperty(SuppSet, collection_name='supp_list')

I'm able to delete the Supp from its original SuppSet, but I can't figure out how to change the SuppSet that a Supp points to. I've tried the following with no success:

q = SuppSet.gql("WHERE name = :1", name_of_the_new_SuppSet)
s = q.get()
supp.suppset = s

I've also tried using list manipulation to push the Supp into the new SuppSet's collection_list supp_list, which didn't work.

Any help is much appreciated.


from google.appengine.ext import db

class SuppSet(db.Model):
    name = db.StringProperty()

class Supp(db.Model):
    suppset = db.ReferenceProperty(SuppSet, collection_name='supp_list')

suppSet0, suppSet1 = SuppSet(name = '0'), SuppSet(name = '1')
suppSet0.put()
suppSet1.put()

supp = Supp(suppset=suppSet0)
supp.put()

print 'suppSet0.supp_list: %r' % list(suppSet0.supp_list)
print 'suppSet1.supp_list: %r' % list(suppSet1.supp_list)
print 'suppset for sup: %s' % supp.suppset.name

supp.suppset = suppSet1
supp.put()

print 'suppSet0.supp_list: %r' % list(suppSet0.supp_list)
print 'suppSet1.supp_list: %r' % list(suppSet1.supp_list)
print 'suppset for sup: %s' % supp.suppset.name

works fine in the interactive console:

suppSet0.supp_list: [<__main__.Supp object at 0x42a0f10>]
suppSet1.supp_list: []
suppset for sup: 0
suppSet0.supp_list: []
suppSet1.supp_list: [<__main__.Supp object at 0x429a3d0>]
suppset for sup: 1
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜