Skip to content

Commit

Permalink
Fix to_dict for IntEnum
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelioLpz committed Oct 7, 2023
1 parent f6128be commit 0aaf4db
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mongoengine_plus/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def mongo_to_dict(obj, exclude_fields: list = None) -> dict:
elif isinstance(obj._fields[field_name], DictField):
return_data[field_name] = data
elif isinstance(obj._fields[field_name], EnumField):
return_data[field_name] = data.value if data else None
return_data[field_name] = data.value if data is not None else None
elif isinstance(obj._fields[field_name], LazyReferenceField):
return_data[f'{field_name}_uri'] = (
f'/{data._DBRef__collection}/{data.id}' if data else None
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.1.0'
__version__ = '0.1.1.dev0'
9 changes: 9 additions & 0 deletions tests/models/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ class EnumType(Enum):
member = 'name'


class IntEnum(int, Enum):
exito = 0
invalid = 1



class Embedded(EmbeddedDocument):
meta = {'allow_inheritance': True}
name = StringField()
Expand Down Expand Up @@ -59,6 +65,7 @@ class TestModel(Document):
lazzy_field = LazyReferenceField(Reference)
lazzy_list_field = ListField(LazyReferenceField(Reference))
generic_lazzy_field = GenericLazyReferenceField()
code = EnumField(IntEnum)

__test__ = False

Expand All @@ -71,6 +78,7 @@ def test_mongo_to_dict():
lazzy_list_field=[reference],
embedded_field=Embedded(name='Peter'),
heritage_field=HeritageEmbedded(name='some', lastname='other'),
code=IntEnum.exito,
)
model.save()
model_dict = mongo_to_dict(model, exclude_fields=['str_field'])
Expand All @@ -95,3 +103,4 @@ def test_mongo_to_dict():
'name': 'some',
'lastname': 'other',
}
assert model_dict['code'] == 0

0 comments on commit 0aaf4db

Please sign in to comment.