File tree Expand file tree Collapse file tree 6 files changed +64
-11
lines changed
Expand file tree Collapse file tree 6 files changed +64
-11
lines changed Original file line number Diff line number Diff line change 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
3535Django 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
191195If features are utilized that require a missing optional dependency an
192196exception will be thrown.
Original file line number Diff line number Diff line change 5656try :
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
6262try :
Original file line number Diff line number Diff 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
142147If features are utilized that require a missing optional dependency an
143148exception will be thrown.
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff 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
295326Filtering
You can’t perform that action at this time.
0 commit comments