Skip to content

Commit 14749cb

Browse files
committed
fix coverage - add docs for DRF integration
1 parent 4b0e487 commit 14749cb

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

README.rst

+10-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
.. _enumerations: https://docs.python.org/3/library/enum.html#enum.Enum
3131
.. _ValueError: https://docs.python.org/3/library/exceptions.html#ValueError
3232
.. _DRY: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
33-
33+
.. _DRF: https://www.django-rest-framework.org
3434

3535
Django Enum
3636
###########
@@ -182,11 +182,15 @@ Installation
182182
.. note::
183183

184184
``django-enum`` has several optional dependencies that are not pulled in
185-
by default. To utilize the
186-
`enum-properties <https://pypi.org/project/enum-properties/>`_ choice types
187-
you must `pip install enum-properties` and to use the ``EnumFilter`` type
188-
for `django-filter <https://pypi.org/project/django-filter/>`_ you
189-
must `pip install django-filter`.
185+
by default. ``EnumFields`` work seamlessly with all Django apps that
186+
work with model fields with choices. Optional integrations are provided
187+
with several popular libraries to extend this basic functionality. To
188+
utilize the `enum-properties <https://pypi.org/project/enum-properties/>`_
189+
choice types you must `pip install enum-properties` and to use the
190+
``EnumFilter`` type for
191+
`django-filter <https://pypi.org/project/django-filter/>`_ you
192+
must `pip install django-filter`. And to use the DRF_ serializer field you
193+
must `pip install djangorestframework`.
190194

191195
If features are utilized that require a missing optional dependency an
192196
exception will be thrown.

django_enum/tests/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
try:
5757
import rest_framework
5858
INSTALLED_APPS.insert(0, 'rest_framework')
59-
except (ImportError, ModuleNotFoundError):
59+
except (ImportError, ModuleNotFoundError): # pragma: no cover
6060
pass
6161

6262
try:

doc/source/index.rst

+9-4
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@ Installation
134134
.. note::
135135

136136
``django-enum`` has several optional dependencies that are not pulled in
137-
by default. To utilize the
138-
enum-properties_ choice types you must `pip install enum-properties` and
139-
to use the ``EnumFilter`` type for django-filter_ you must
140-
`pip install django-filter`.
137+
by default. ``EnumFields`` work seamlessly with all Django apps that
138+
work with model fields with choices. Optional integrations are provided
139+
with several popular libraries to extend this basic functionality. To
140+
utilize the `enum-properties <https://pypi.org/project/enum-properties/>`_
141+
choice types you must `pip install enum-properties` and to use the
142+
``EnumFilter`` type for
143+
`django-filter <https://pypi.org/project/django-filter/>`_ you
144+
must `pip install django-filter`. And to use the DRF_ serializer field you
145+
must `pip install djangorestframework`.
141146

142147
If features are utilized that require a missing optional dependency an
143148
exception will be thrown.

doc/source/reference.rst

+11
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,14 @@ Forms
4545
:undoc-members:
4646
:show-inheritance:
4747
:private-members:
48+
49+
50+
Serializer Fields
51+
-----------------
52+
53+
.. automodule:: django_enum.drf
54+
:members:
55+
:undoc-members:
56+
:show-inheritance:
57+
:private-members:
58+

doc/source/refs.rst

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
.. _DRY: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
1515
.. _enum-properties: https://pypi.org/project/enum-properties
1616
.. _django-filter: https://pypi.org/project/django-filter
17+
.. _DRF: https://www.django-rest-framework.org
18+

doc/source/usage.rst

+31
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,37 @@ would define our form like so:
290290
</select>
291291

292292

293+
.. _rest_framework:
294+
295+
Django Rest Framework
296+
#####################
297+
298+
By default DRF_ ``ModelSerializer`` will use a ``ChoiceField`` to represent an
299+
``EnumField``. This works great, but it will not accept symmetric enumeration
300+
values. A serializer field ``EnumField`` is provided that will. The dependency
301+
on DRF_ is optional so to use the provided serializer field you must install
302+
DRF_:
303+
304+
.. code:: bash
305+
306+
pip install djangorestframework
307+
308+
.. code-block::
309+
310+
from django_enum.drf import EnumField
311+
from rest_framework import serializers
312+
313+
class ExampleSerializer(serializers.Serializer):
314+
315+
color = EnumField(TextChoicesExample.Color)
316+
317+
ser = ExampleSerializer(data={'color': (1, 0, 0)})
318+
assert ser.is_valid()
319+
320+
The serializer ``EnumField`` accepts any arguments that ``ChoiceField``. It
321+
also accepts the ``strict`` parameter that behaves the same as the model
322+
field's ``strict`` parameter.
323+
293324
.. _filtering:
294325

295326
Filtering

0 commit comments

Comments
 (0)