Skip to content

Instantly share code, notes, and snippets.

@kibernick
Created February 10, 2014 11:40
Show Gist options
  • Save kibernick/8914403 to your computer and use it in GitHub Desktop.
Save kibernick/8914403 to your computer and use it in GitHub Desktop.
MongoDB in Python through MongoEngine - playing with DynamicDocuments and instantiating different types of Documents in the same collection
from mongoengine import Document, DynamicDocument
class AppelateCase(Document):
appelate_blob = StringField()
class BankruptcyCase(Document):
bankrupcy_blob = StringField()
class MyCase(DynamicDocument):
type = StringField()
@classmethod
def from_appelate(cls, appelate_case):
mc = cls.from_json(appelate_case.to_json())
mc.type = "AppelateCase"
return mc
@classmethod
def from_bankrupcy(cls, bankrupcy_case):
mc = cls.from_json(bankrupcy_case.to_json())
mc.type = "BankruptcyCase"
return mc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment