From 1908356b688c94ad76390ba0576f563c5e2e65b0 Mon Sep 17 00:00:00 2001 From: Alex Viquez Date: Wed, 26 Oct 2022 19:17:45 -0500 Subject: [PATCH] _cls field remove from mongo_to_dict (#69) * cheve * roger knows --- mongoengine_plus/models/helpers.py | 3 +++ mongoengine_plus/version.py | 2 +- requirements-test.txt | 2 +- tests/models/test_helpers.py | 17 +++++++++++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mongoengine_plus/models/helpers.py b/mongoengine_plus/models/helpers.py index d947213..4925eab 100644 --- a/mongoengine_plus/models/helpers.py +++ b/mongoengine_plus/models/helpers.py @@ -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: diff --git a/mongoengine_plus/version.py b/mongoengine_plus/version.py index ffcc925..156d6f9 100644 --- a/mongoengine_plus/version.py +++ b/mongoengine_plus/version.py @@ -1 +1 @@ -__version__ = '0.0.3' +__version__ = '0.0.4' diff --git a/requirements-test.txt b/requirements-test.txt index e41bcdb..995c245 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,4 +1,4 @@ -black==21.5b1 +black==22.3.0 flake8==3.9.* isort==5.8.* mypy==0.812 diff --git a/tests/models/test_helpers.py b/tests/models/test_helpers.py index 99c4823..b6a7b89 100644 --- a/tests/models/test_helpers.py +++ b/tests/models/test_helpers.py @@ -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() @@ -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() @@ -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']) @@ -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', + }