ReferenceFields and Inheritance

88 views
Skip to first unread message

Sid

unread,
Feb 22, 2012, 10:37:05 PM2/22/12
to MongoEngine Users
Hi - I want to know whether I can assign a subclass reference to a
referencefield to the constructor of which the base class has been
passed. For example:

class Base(Document):
str1=StringField(max_length=50)

class Derived(Base):
str2=StringField(max_length=50)


class Container(Document):
r=ReferenceField(Base)


d=Derived(str1="str1",str2="str2")
c=Container()
c.r=d

Is the line c.r=d valid?

Or do I need to use a GenericReferenceField?

Ross Lawley

unread,
Feb 23, 2012, 7:12:59 AM2/23/12
to mongoeng...@googlegroups.com
Hi,

Thats fine, just need to make sure you save `d` the derived instance before setting / saving the container.

Ross
Reply all
Reply to author
Forward
0 new messages