Skip to content

Instantly share code, notes, and snippets.

@ikonst
Created December 19, 2017 20:09
Show Gist options
  • Save ikonst/4c096d361d31fd841e815d19a9ef04eb to your computer and use it in GitHub Desktop.
Save ikonst/4c096d361d31fd841e815d19a9ef04eb to your computer and use it in GitHub Desktop.
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