Skip to content

shosca/django-rest-enumfield

Repository files navigation

Django REST EnumField

Build Status PyPI version Coveralls Status Black

EnumField that uses python enums for Django REST Framework

Installation

pip install django-rest-enumfield

Usage

Use it as if its a ChoiceField:

import enum
from rest_enumfield import EnumField

class Color(enum.Enum):
   RED = "red"
   GREEN = "green"
   BLUE = "blue"

class SomeSerializer(Serializer):

   color = EnumField(choices=Color)

Additionally you can override choice name and value generation by providing to_choice or to_repr arguments:

class SomeSerializer(Serializer):

   color = EnumField(choices=Color, to_choice=lambda x: (x.value, x.name), to_repr=lambda x: x.value)

This will cause the enum's value instead of the name to be represented.

Thats it.

About

EnumField for Django REST Framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •