Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One doesn't just add "allow_inheritance=True"
from mongoengine import Document, StringField, ListField, EmbeddedDocument, EmbeddedDocumentField
# ******** before remodel: **********
class FailedChargeAttempt_Old(EmbeddedDocument):
id = StringField()
failed_reason = StringField()
class ChargeModel_Old(Document):
attempts = ListField(EmbeddedDocumentField(FailedChargeAttempt_Old))
charge_old = ChargeModel_Old()
charge_old.attempts.append(FailedChargeAttempt_Old(id='blah', failed_reason='extended data'))
# ******** after remodel: **********
class BaseChargeAttempt(EmbeddedDocument):
meta = {
'allow_inheritance': True
}
id = StringField()
class FailedChargeAttempt(BaseChargeAttempt):
failed_reason = StringField()
class ChargeModel(Document):
attempts = ListField(EmbeddedDocumentField(BaseChargeAttempt))
# ******** let's test *************
old_son = charge_old.to_mongo()
charge = ChargeModel._from_son(old_son)
print(charge.attempts[0])
print(charge.attempts[0].to_mongo()['_cls'])
print(charge.attempts[0].failed_reason)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment