Extension for Django REST Framework 3 which allows for using EnumField from https://github.com/hzdg/django-enumfields.
pip install drf-enum-field
- Python 2.7+
- Django 1.6+
- Django REST Framework 3.9
- django-enumfields 0.9+
By using the EnumFieldSerializerMixin EnumFields are serialized and deserialized correctly.
class TestResource(models.Model):
class Type(Enum):
DEFAULT = 'DEFAULT'
NON_DEFAULT = 'NON_DEFAULT'
type = EnumField(Type)
name = models.CharField(max_length=255)
class TestResourceSerializer(EnumFieldSerializerMixin, ModelSerializer):
class Meta:
model = TestResource
class TestResourceViewSet(ModelViewSet):
serializer_class = TestResourceSerializer
queryset = TestResource.objects.all()
{
"id": 1,
"name": "Test-Resource",
"type": "DEFAULT"
}