Skip to content

Commit

Permalink
Fix value_to_string to return string always
Browse files Browse the repository at this point in the history
  • Loading branch information
gsakkis committed Aug 9, 2020
1 parent 0eabdb4 commit b918ae8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
8 changes: 1 addition & 7 deletions enumfields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,8 @@ def from_db_value(self, value, expression, connection, *args):
return self.to_python(value)

def value_to_string(self, obj):
"""
This method is needed to support proper serialization. While its name is value_to_string()
the real meaning of the method is to convert the value to some serializable format.
Since most of the enum values are strings or integers we WILL NOT convert it to string
to enable integers to be serialized natively.
"""
value = self.value_from_object(obj)
return value.value if value else None
return str(value.value) if value is not None else ''

def get_default(self):
if self.has_default():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_django_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def test_serialization():
ser = PythonSerializer()
ser.serialize([m])
fields = ser.getvalue()[0]["fields"]
assert fields["color"] == m.color.value
assert fields["taste"] == m.taste.value
assert fields["color"] == "r"
assert fields["taste"] == "4"


@pytest.mark.django_db
Expand Down

0 comments on commit b918ae8

Please sign in to comment.