Skip to content

Commit

Permalink
added LazyReferenceField query support
Browse files Browse the repository at this point in the history
  • Loading branch information
aiscenblue committed Apr 22, 2018
1 parent 1319c4c commit d26153c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions mongoengine_serialize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mongoengine.base import BaseDocument
from mongoengine.base.datastructures import LazyReference
from bson.objectid import ObjectId
from datetime import datetime
from mongoengine.queryset.queryset import QuerySet
Expand Down Expand Up @@ -36,6 +37,9 @@ def __call__(self, collections):
def __serialize_type_of(self, collection):
if isinstance(collection, BaseDocument):
return self.__filter_serialize(collection.to_mongo(), collection)
elif isinstance(collection, LazyReference):
raw = collection.fetch()
return self.__filter_serialize(raw.to_mongo(), raw)
elif isinstance(collection, JsonSerialized):
return collection
else:
Expand All @@ -60,14 +64,19 @@ def __filter_serialize(self, collections, raw):
for index, collection in enumerate(collections.items()):
key, value = collection
raw_collection = getattr(raw, self.__get_raw_name(key), collection)
if isinstance(value, list) or isinstance(value, dict):
re_serialize = Serialize(raw_collection).jsonify()
new_collection.update(dict.fromkeys((key,), re_serialize))
if isinstance(value, list):
serialized_list = list()
for _ in value:
serialized_list.append(self.__filter_serialize(_, raw_collection))
new_collection.update(dict.fromkeys((key,), serialized_list))
elif isinstance(value, dict):
serialized_dict = self.__filter_serialize(collection, raw_collection)
new_collection.update(dict.fromkeys((key,), serialized_dict))
else:
new_collection.update(self.__serialize(key, value, raw_collection))
return new_collection
else:
return collections
return self.__to_string_attribute(collections)

def __serialize(self, key, value, raw):
serialized_attribute = self.__attribute_serialize(key, value)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='mongoengine-serialize',
version='1.2.3',
version='1.2.4',
description='Mongoengine serializer',
author='Jeffrey Marvin Forones',
author_email='aiscenblue@gmail.com',
Expand Down

0 comments on commit d26153c

Please sign in to comment.