def to_dict(document: Document): raw = BsonEncoder().encode(document.to_mongo()) document = loads(raw) fields = ['_id', 'inserted_at', 'updated_at', 'is_deleted'] for key in document.copy().keys(): if key in fields: del document[key] return document
def doc_to_dict(doc: Document): """Converts a mongoengine document into a json serializable dictionary. This implementation is naive and doesnt take into account name aliases. It may also have issues with certain field types If the above potentials become a problem, see https://gist.github.com/jason-w/4969476 :param mongoengine.Document doc: A mongo document extracted through or created from mongoengine :returns dict: """ retval: dict = doc.to_mongo() if '_id' in retval: retval['_id'] = str(retval['_id']) return retval
def render_document(doc: Document, view: BaseModel, exclude: Set[str] = None): """Converts a mongoengine document into a json serializable dictionary. This is not great. :param mongoengine.Document doc: A mongo document extracted through or created from mongoengine. :param pydantic.BaseModel view: The view to use to serialize the data. :param set exclude: Any fields to exclude. :returns dict: """ data = view(**doc.to_mongo()).dict(exclude=exclude) # # cant handle nested objects # for key, value in data.items(): # if isinstance(value, ObjectId): # print('found object id') # data[key] = str(value) return data
def update_by_document(done_trade: mongoengine.Document): dic = filter_dict_by_id(done_trade.to_mongo().to_dict()) done_trade.update(**dic)