Skip to content

Commit

Permalink
_cls field remove from mongo_to_dict (#69)
Browse files Browse the repository at this point in the history
* cheve

* roger knows
  • Loading branch information
alexviquez authored Oct 27, 2022
1 parent 01e55db commit 1908356
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions mongoengine_plus/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def mongo_to_dict(obj, exclude_fields: list = None) -> dict:
if exclude_fields is None:
exclude_fields = []

# remove _cls from heritage embedded fields

exclude_fields.append('_cls')
for field_name in obj._fields:

if field_name in exclude_fields:
Expand Down
2 changes: 1 addition & 1 deletion mongoengine_plus/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.3'
__version__ = '0.0.4'
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black==21.5b1
black==22.3.0
flake8==3.9.*
isort==5.8.*
mypy==0.812
Expand Down
17 changes: 15 additions & 2 deletions tests/models/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ class EnumType(Enum):


class Embedded(EmbeddedDocument):
meta = {'allow_inheritance': True}
name = StringField()


class HeritageEmbedded(Embedded):
lastname = StringField()


class TestModel(Document):
id = StringField(default=uuid_field('PK'), primary_key=True)
str_field = StringField()
Expand All @@ -50,6 +55,7 @@ class TestModel(Document):
enum_list_field = ListField(EnumField(EnumType), default=[EnumType.member])
embedded_list_field = ListField(EmbeddedDocumentField(Embedded))
embedded_field = EmbeddedDocumentField(Embedded)
heritage_field = EmbeddedDocumentField(HeritageEmbedded)
lazzy_field = LazyReferenceField(Reference)
lazzy_list_field = ListField(LazyReferenceField(Reference))
generic_lazzy_field = GenericLazyReferenceField()
Expand All @@ -61,7 +67,10 @@ def test_mongo_to_dict():
reference = Reference()
reference.save()
model = TestModel(
embedded_list_field=[Embedded(name='')], lazzy_list_field=[reference]
embedded_list_field=[Embedded(name='')],
lazzy_list_field=[reference],
embedded_field=Embedded(name='Peter'),
heritage_field=HeritageEmbedded(name='some', lastname='other'),
)
model.save()
model_dict = mongo_to_dict(model, exclude_fields=['str_field'])
Expand All @@ -78,7 +87,11 @@ def test_mongo_to_dict():
assert model_dict['list_field'] == ['42']
assert model_dict['enum_list_field'] == ['name']
assert model_dict['embedded_list_field'] == [{'name': ''}]
assert model_dict['embedded_field'] == {}
assert model_dict['embedded_field'] == {'name': 'Peter'}
assert model_dict['lazzy_field_uri'] is None
assert model_dict['generic_lazzy_field_uri'] is None
assert model_dict['lazzy_list_field_uris'] == ["Reference object"]
assert model_dict['heritage_field'] == {
'name': 'some',
'lastname': 'other',
}

0 comments on commit 1908356

Please sign in to comment.