Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/shosca/django-rest-enumfield
- Owner: shosca
- License: mit
- Created: 2019-06-28T11:14:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-05T21:52:45.000Z (3 months ago)
- Last Synced: 2024-10-06T10:47:39.580Z (about 1 month ago)
- Topics: django, django-rest-framework, enum, rest
- Language: Python
- Homepage:
- Size: 38.1 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- License: LICENSE
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 EnumFieldclass 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