Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with django-rest-framework: Enum is not JSON serializable #30

Closed
jessamynsmith opened this issue Feb 28, 2015 · 12 comments
Closed

Comments

@jessamynsmith
Copy link

I am trying to create an API using django-rest-framework, on a model object which has fields of type EnumIntegerField. When I make an API call, I get an error that the Enum is not JSON serializable.

Traceback:
File "/Users/jessamyn/.virtualenvs/eggtimer/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/Users/jessamyn/.virtualenvs/eggtimer/lib/python3.4/site-packages/django/template/response.py" in render
  103.             self.content = self.rendered_content
File "/Users/jessamyn/.virtualenvs/eggtimer/lib/python3.4/site-packages/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/Users/jessamyn/.virtualenvs/eggtimer/lib/python3.4/site-packages/rest_framework/renderers.py" in render
  98.             separators=separators
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py" in dumps
  237.         **kw).encode(obj)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in encode
  192.         chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in iterencode
  250.         return _iterencode(o, 0)
File "/Users/jessamyn/.virtualenvs/eggtimer/lib/python3.4/site-packages/rest_framework/utils/encoders.py" in default
  63.         return super(JSONEncoder, self).default(obj)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/encoder.py" in default
  173.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /api/v2/periods/
Exception Value: <FlowLevel.MEDIUM: 2> is not JSON serializable
@matthewwithanm
Copy link
Contributor

Hm, we're using this with DRF but Python 2.7. Do you think you could put together a small test for this? It shouldn't need DRF—my guess is that just trying to serialize the model will trigger it. All the tests are being run in 3.4 with tox so that would make it really easy for us to dive in. Either way, thanks for the report!

@jessamynsmith
Copy link
Author

I added pull request #31
I'm a bit mystified you don't see an error on python 2.7. Perhaps it somehow avoids this path entirely.

@jessamynsmith
Copy link
Author

I tried running my project on python 2.7 and I still see the serializer error. Perhaps there is something wrong with my definitions?
Here are my models: https://github.com/jessamynsmith/eggtimer/blob/master/periods/models.py#L74
and serializers: https://github.com/jessamynsmith/eggtimer/blob/django_enumfields/periods/serializers.py

Also, here is the test that works on master, with django-enumfield:
https://github.com/jessamynsmith/eggtimer/blob/master/periods/tests/test_serializers.py
and errors out on the branch, with django-enumfields:
https://github.com/jessamynsmith/eggtimer/blob/django_enumfields/periods/tests/test_serializers.py

@c4traz
Copy link

c4traz commented Dec 2, 2015

I could work around it by using drf-enum-field:

from drf_enum_field.serializers import EnumFieldSerializerMixin
class VehicleSerializer(EnumFieldSerializerMixin, serializers.ModelSerializer): 
...

@hacknaked
Copy link

I use a custom DRF field for this:

models.py

from enum import Enum
from django.db import models
from enumfields import EnumIntegerField

class GeoModel(models.Model):

    class LocationType(Enum):
        ROOFTOP = 1
        RANGE_INTERPOLATED = 2
        GEOMETRIC_CENTER = 3
        APPROXIMATE = 4
        UNRESOLVED = 5

    location_type = EnumIntegerField(
        enum=LocationType,
        default=LocationType.UNRESOLVED
    )
    ...

fields.py

from rest_framework import serializers


class EnumField(serializers.ChoiceField):
    def __init__(self, enum, **kwargs):
        self.enum = enum
        kwargs['choices'] = [(e.name, e.name) for e in enum]
        super(EnumField, self).__init__(**kwargs)

    def to_representation(self, obj):
        return obj.name

    def to_internal_value(self, data):
        try:
            return self.enum[data]
        except KeyError:
            self.fail('invalid_choice', input=data)

serializers.py

from rest_framework import serializers
from . import fields
from . import models

class GeoModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.GeoModel

    location_type = fields.EnumField(enum=models.GeoModel.LocationType)

@aelavender
Copy link

Sorry to necro an issue.

I just wrote a DRF serializer field, then looked here to see that @hacknaked wrote almost exactly the same thing.

Proposal: add that code to this project and make DRF automagically use it. Something along the lines of:

def _make_drf_enum_field():
    import enumfields
    from distutils.version import StrictVersion

    try:
        import rest_framework
    except ImportError:
        return  # DRF not installed

    if StrictVersion(rest_framework.__version__) < StrictVersion('3.0'):
        return  # DRF version too old

    class EnumField:
        (the code above)

    # Patch DRF
    rest_framework.serializers.ModelSerializer.serializer_field_mapping[enumfields.EnumField] = EnumField
    rest_framework.serializers.ModelSerializer.serializer_field_mapping[enumfields.EnumIntegerField] = EnumField

    return EnumField

EnumField = _make_drf_enum_field()
if EnumField is None:
    del EnumField

cc: @tomchristie -- Is there a supported way of registering new mappings like this?

@tomchristie
Copy link

There's no automagical way for custom fields to declare how they should be mapped to serializer fields, no. (Out of interest what serializer field does it create? print a ModelSerializer to find out). It's not impossible that we could think about adding an API for this if we knew it was going to get uptake from third party modelfield implementations.

@aelavender
Copy link

Thanks for the quick response, Tom! They create ChoiceFields.

@sdementen
Copy link

@aelavender I have tried the solution hereabove yet there are issue serializing the field due to line https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L1214 that override the field_class got from rest_framework.serializers.ModelSerializer.serializer_field_mapping.
Is it critical for enumfields to have the field with the choices named choices? or can we rename this field to choices_enum to avoid triggering line https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L1211 ?

@sdementen
Copy link

sdementen commented Mar 7, 2019

I can make it working by replacing the line https://github.com/hzdg/django-enumfields/blob/master/enumfields/fields.py#L43 to
(i.value, getattr(i, 'label', i.name))
without needing the code above but adding to my Enum type:

    def __str__(self):
        return self.value

@louwers
Copy link

louwers commented Nov 5, 2019

We just ran into this bug/omission, drf-yasg (our documentation generator) is crashing because of it.

A fix would be appreciated.

@akx
Copy link
Contributor

akx commented Jan 16, 2020

DRF serializers referring to models with EnumFields should either

  • use enumfields.drf.fields.EnumField serializer fields for enum model fields, or
  • add the EnumSupportSerializerMixin that teaches ModelSerializers to use the above serializer field for EnumFields.

That should fix these issues. Feel free to comment and/or open a new issue if I missed something here! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants