@@ -84,8 +84,13 @@ By default `DRF ModelSerializer
84
84
`ChoiceField <https://www.django-rest-framework.org/api-guide/fields/#choicefield >`_ to represent an
85
85
:class: `~django_enum.fields.EnumField `. This works great, but it will not accept :ref: `symmetric
86
86
enumeration values <enum-properties:howto_symmetric_properties>`. A serializer field
87
- :class: `~django_enum.drf.EnumField ` is provided that will. The dependency on DRF _ is optional so
88
- to use the provided serializer field you must install DRF _:
87
+ :class: `django_enum.drf.EnumField ` is provided that will. :class: `~django_enum.fields.FlagField `
88
+ fields do not work well with DRF's builtin
89
+ `MultipleChoiceField <https://www.django-rest-framework.org/api-guide/fields/#multiplechoicefield >`_
90
+ so we provide also provide a :class: `django_enum.drf.FlagField `.
91
+
92
+ The dependency on DRF _ is optional so to use the provided serializer field you must install DRF _:
93
+
89
94
90
95
.. code :: bash
91
96
@@ -97,7 +102,7 @@ to use the provided serializer field you must install DRF_:
97
102
98
103
99
104
100
- The serializer :class: `~ django_enum.drf.EnumField ` accepts any arguments that
105
+ The serializer :class: `django_enum.drf.EnumField ` accepts any arguments that
101
106
`ChoiceField <https://www.django-rest-framework.org/api-guide/fields/#choicefield >`_ does. It also
102
107
accepts the ``strict `` parameter which behaves the same way as it does on the model field.
103
108
@@ -110,14 +115,28 @@ accepts the ``strict`` parameter which behaves the same way as it does on the mo
110
115
2. You have non-strict model fields and want to allow your API to accept values outside of
111
116
the enumeration.
112
117
118
+ The :class: `django_enum.drf.EnumField ` must be used for any :class: `~django_enum.fields.FlagField `
119
+ fields. It will accept a composite integer or a list of any values coercible to a flag. The
120
+ serialized output will be an composite integer holding the full bitfield.
121
+
122
+ ModelSerializers
123
+ ~~~~~~~~~~~~~~~~
124
+
125
+ An :class: `django_enum.drf.EnumFieldMixin ` class is provided that when added to
126
+ `ModelSerializers <https://www.django-rest-framework.org/api-guide/serializers/#modelserializer >`_
127
+ will be sure that the serializer instantiates the correct django-enum serializer field type:
128
+
129
+ .. literalinclude :: ../../../tests/examples/drf_modelserializer_howto.py
130
+ :lines: 3-
131
+
113
132
.. _filtering :
114
133
115
134
django-filter
116
135
-------------
117
136
118
137
As shown above, filtering by any value, enumeration type instance or symmetric value works with
119
138
:doc: `Django's ORM <django:topics/db/queries >`. This is not natively true for the default
120
- :doc: ` FilterSet < django-filter:ref/ filterset> ` from :doc: `django-filter <django-filter:index >`.
139
+ :class: ` django_filters. filterset.FilterSet ` from :doc: `django-filter <django-filter:index >`.
121
140
Those filter sets will only be filterable by direct enumeration value by default. An
122
141
:class: `~django_enum.filters.EnumFilter ` class is provided to enable filtering by symmetric property
123
142
values, but since the dependency on :doc: `django-filter <django-filter:index >` is optional, you must
@@ -128,11 +147,24 @@ first install it:
128
147
pip install django-filter
129
148
130
149
.. literalinclude :: ../../../tests/examples/filterfield_howto.py
131
- :lines: 3 -
150
+ :lines: 2 -
132
151
133
- An :class: `~django_enum.filters.FilterSet ` class is also provided that uses
152
+ A :class: `~django_enum.filters.FilterSet ` class is also provided that uses
134
153
:class: `~django_enum.filters.EnumFilter ` for :class: `~django_enum.fields.EnumField ` by default.
135
154
So the above is also equivalent to:
136
155
137
156
.. literalinclude :: ../../../tests/examples/filterset_howto.py
138
157
:lines: 3-
158
+
159
+ .. tip ::
160
+
161
+ :class: `~django_enum.filters.FilterSet ` may also be used as a mixin.
162
+
163
+
164
+ FlagFields
165
+ ~~~~~~~~~~
166
+
167
+ An :class: `~django_enum.filters.EnumFlagFilter ` field for flag fields is also provided:
168
+
169
+ .. literalinclude :: ../../../tests/examples/flagfilterfield_howto.py
170
+ :lines: 2-
0 commit comments