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

Add documentation for django rest framework integration #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,42 @@ class PersonForm(forms.Form):
Rendering `PersonForm` in a template will generate a select-box with "Male" and "Female" as option labels for the gender field.


### Django Rest Framework
`contrib.drf.NamedEnumField` and `contrib.drf.EnumField` classes are provided as serializer fields for the rest framework.

```python
from django.db import models
from django_enumfield import enum
from django_enumfield.contrib import drf
from rest_framework import serializers


class DeliveryStatus(enum.Enum):
NEW = 1
IN_PROGRESS = 2
COMPLETE = 3


class Parcel(models.Model):
status = enum.EnumField(DeliveryStatus)


class ParcelSerializer(serializers.ModelSerializer):
name = drf.NamedEnumField(DeliveryStatus, default=DeliveryStatus.NEW)
value = drf.EnumField(DeliveryStatus, default=DeliveryStatus.COMPLETE)

class Meta:
model = Parcel
fields = ('name', 'value')

# The returned json will be
{
'named': 'NEW',
'value': 3
}
```


Local Development Environment
-----------------------------

Expand Down