gpt4 book ai didi

python - GAE python - 如何更改 "one"对象指向的 "many"?

转载 作者:行者123 更新时间:2023-12-01 06:12:38 25 4
gpt4 key购买 nike

我正在使用 GAE 数据库来存储 Supp 类型的对象,这些对象是 SuppSet 的一部分。一个 SuppSet 中可以有许多 Supp。我使用 ReferenceProperty 模型在 SuppSet 和 Supps 之间创建一对多关系,如下所示:

class SuppSet(db.Model):
<stuff>

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

我可以从原始 SuppSet 中删除 Supp,但我不知道如何更改该 SuppSet Supp 指向。我尝试了以下方法但没有成功:

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

我还尝试使用列表操作将Supp推送到新的SuppSet的collection_listsupp_list中>,这不起作用。

非常感谢任何帮助。

最佳答案

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

在交互式控制台中工作正常:

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

关于python - GAE python - 如何更改 "one"对象指向的 "many"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4965033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com