Created
December 19, 2017 20:09
-
-
Save ikonst/4c096d361d31fd841e815d19a9ef04eb to your computer and use it in GitHub Desktop.
One doesn't just add "allow_inheritance=True"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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