Saturday, December 20, 2008

Polymorphism in AppEngine Datastore Models - continued

Playing around with the Master and Details classes in the previous post, I tried to subclass a Master this time:
class MoreMaster(Master):
 mmp = db.StringProperty()

mm=MoreMaster(mp='f', mmp='g')
mm.put()

d4 = Detail(dp='h', master=mm)
d4.put()

for d in mm.detail_set:
 print d.master.mmp
It printed 'g', thus resolving correctly that d4.master is derived class. So the "many to one" relation supports polymorphism, but "one to many" does not. The reason is db.Key already contains the name of the Model class. So when we do Master.get('LongLongKeyHere') the Datastore is able to create the correct class.

No comments: