Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shosca/django-rest-enumfield

EnumField for Django REST Framework
https://github.com/shosca/django-rest-enumfield

django django-rest-framework enum rest

Last synced: 8 days ago
JSON representation

EnumField for Django REST Framework

Awesome Lists containing this project

README

        

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``:

.. code:: python

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:

.. code:: python

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.

.. |Build Status| image:: https://github.com/shosca/django-rest-enumfield/workflows/Build/badge.svg?branch=master
:target: https://github.com/shosca/django-rest-enumfield/actions?query=workflow%3ABuild+branch%3Amaster
.. |PyPI version| image:: https://badge.fury.io/py/django-rest-enumfield.svg
:target: https://badge.fury.io/py/django-rest-enumfield
.. |Coveralls Status| image:: https://coveralls.io/repos/github/shosca/django-rest-enumfield/badge.svg?branch=master
:target: https://coveralls.io/github/shosca/django-rest-enumfield?branch=master
.. |Black| image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/ambv/black