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?
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):