File tree 6 files changed +64
-11
lines changed
6 files changed +64
-11
lines changed Original file line number Diff line number Diff line change 30
30
.. _enumerations : https://docs.python.org/3/library/enum.html#enum.Enum
31
31
.. _ValueError : https://docs.python.org/3/library/exceptions.html#ValueError
32
32
.. _DRY : https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
33
-
33
+ .. _ DRF : https://www.django-rest-framework.org
34
34
35
35
Django Enum
36
36
###########
@@ -182,11 +182,15 @@ Installation
182
182
.. note ::
183
183
184
184
``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 `.
190
194
191
195
If features are utilized that require a missing optional dependency an
192
196
exception will be thrown.
Original file line number Diff line number Diff line change 56
56
try :
57
57
import rest_framework
58
58
INSTALLED_APPS .insert (0 , 'rest_framework' )
59
- except (ImportError , ModuleNotFoundError ):
59
+ except (ImportError , ModuleNotFoundError ): # pragma: no cover
60
60
pass
61
61
62
62
try :
Original file line number Diff line number Diff line change @@ -134,10 +134,15 @@ Installation
134
134
.. note ::
135
135
136
136
``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 `.
141
146
142
147
If features are utilized that require a missing optional dependency an
143
148
exception will be thrown.
Original file line number Diff line number Diff line change 45
45
:undoc-members:
46
46
:show-inheritance:
47
47
: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 14
14
.. _DRY : https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
15
15
.. _enum-properties : https://pypi.org/project/enum-properties
16
16
.. _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:
290
290
</select >
291
291
292
292
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
+
293
324
.. _filtering :
294
325
295
326
Filtering
You can’t perform that action at this time.
0 commit comments