GAE/Python:collection_name 不适用于 Polymodel?

发布于 2024-12-07 09:57:11 字数 1127 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-12-14 09:57:11

因此,查看 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):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文