GAE/Python:collection_name 不适用于 Polymodel?
我在 GAE 中将 db.ReferenceProperty 与 PolyModel 一起使用时遇到问题。如果我有两个从基础 PolyModel 派生的模型,并且每个模型都引用另一个“容器”模型,则容器上的集合包含两个 PolyModel,而不仅仅是您期望的一个。这是一个显示问题的测试用例:
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
class MyContainer(db.Model):
name = db.StringProperty(default='mycontainer', multiline=False)
class MyBaseModel(polymodel.PolyModel):
name = db.StringProperty(default='mybasemodel', multiline=False)
class MyModelOne(MyBaseModel):
container = db.ReferenceProperty(MyContainer, collection_name='model_ones')
class MyModelTwo(MyBaseModel):
container = db.ReferenceProperty(MyContainer, collection_name='model_twos')
print "Beginning test.\n"
c = MyContainer(name="Container")
c.put()
one = MyModelOne(name="One", container=c)
two = MyModelTwo(name="Two", container=c)
one.put()
two.put()
print "Ones:"
for o in c.model_ones:
print o.name
print "\nTwos:"
for o in c.model_twos:
print o.name
输出:
Beginning test.
Ones:
One
Two
Twos:
One
Two
我在设置 ReferenceProperties 时是否遗漏了某些内容,或者这是预期的行为?
I'm having trouble using db.ReferenceProperty with PolyModel in GAE. If I have two models derived from a base PolyModel, and each one has a reference to another "container" model, the collections on the container contain both PolyModels, not just the one as you would expect. Here is a test case that shows the problem:
from google.appengine.ext import db
from google.appengine.ext.db import polymodel
class MyContainer(db.Model):
name = db.StringProperty(default='mycontainer', multiline=False)
class MyBaseModel(polymodel.PolyModel):
name = db.StringProperty(default='mybasemodel', multiline=False)
class MyModelOne(MyBaseModel):
container = db.ReferenceProperty(MyContainer, collection_name='model_ones')
class MyModelTwo(MyBaseModel):
container = db.ReferenceProperty(MyContainer, collection_name='model_twos')
print "Beginning test.\n"
c = MyContainer(name="Container")
c.put()
one = MyModelOne(name="One", container=c)
two = MyModelTwo(name="Two", container=c)
one.put()
two.put()
print "Ones:"
for o in c.model_ones:
print o.name
print "\nTwos:"
for o in c.model_twos:
print o.name
And the output:
Beginning test.
Ones:
One
Two
Twos:
One
Two
Am I missing something in setting up my ReferenceProperties, or is this expected behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,查看 SDK 源代码,似乎 _ReverseReferenceProperty(将集合转换为查询对象)不会尝试处理 PolyModel。 此处有一些关于问题跟踪器的相关投诉此处。
IMO,如果这是一个已知的限制,则应该在 PolyModel 文档中的某处提及。
编辑:无集合的解决方法如下所示:
for o in MyModelOne.all().filter('container =', c):
So looking at the SDK source, it appears that _ReverseReferenceProperty (which translates collections into Query objects) doesn't make any attempt to handle PolyModels. There are a few related complaints on the issue tracker, here and here.
IMO, if this is a known limitation it ought to be mentioned somewhere in the PolyModel docs.
Edit: The collection-less workaround would be something like this:
for o in MyModelOne.all().filter('container =', c):