@@ -163,11 +163,15 @@ def filter(self, qs, value):
163
163
164
164
class FilterSet (filterset .FilterSet ):
165
165
"""
166
- Use this class instead of the :doc:`django-filter <django-filter:index>`
167
- :class:`~django_filters.filterset.FilterSet` to automatically set all
168
- :class:`~django_enum.fields.EnumField` filters to
169
- :class:`~django_enum.filters.EnumFilter` by default instead of
170
- :class:`~django_filters.filters.ChoiceFilter`.
166
+ This filterset behaves the same way as the :doc:`django-filter <django-filter:index>`
167
+ :class:`~django_filters.filterset.FilterSet` except the following fields will be set
168
+ to the following filter types:
169
+
170
+ * :class:`~django_enum.fields.EnumField` -> :class:`~django_enum.filters.EnumFilter`
171
+ * :class:`~django_enum.fields.FlagField` -> :class:`~django_enum.filters.FlagFilter`
172
+
173
+ **If you have a custom** :class:`~django_filters.filterset.FilterSet`
174
+ **implementation, this class can also be used as a mixin.**
171
175
"""
172
176
173
177
@staticmethod
@@ -178,11 +182,11 @@ def enum_extra(f: EnumField) -> t.Dict[str, t.Any]:
178
182
** {
179
183
FlagField : {
180
184
"filter_class" : EnumFlagFilter ,
181
- "extra" : enum_extra ,
185
+ "extra" : lambda f : FilterSet . enum_extra ( f ), # TODO 3.9 compat
182
186
},
183
187
EnumField : {
184
188
"filter_class" : EnumFilter ,
185
- "extra" : enum_extra ,
189
+ "extra" : lambda f : FilterSet . enum_extra ( f ), # TODO 3.9 compat
186
190
},
187
191
},
188
192
** filterset .FilterSet .FILTER_DEFAULTS ,
@@ -193,6 +197,8 @@ def filter_for_lookup(
193
197
cls , field : ModelField , lookup_type : str
194
198
) -> t .Tuple [t .Optional [t .Type [Filter ]], t .Dict [str , t .Any ]]:
195
199
"""For EnumFields use the EnumFilter class by default"""
200
+ # we can't just pass this up to the base implementation because if it sees
201
+ # choices on a field it will hard set to ChoiceField
196
202
if isinstance (field , EnumField ):
197
203
data = (
198
204
try_dbfield (
@@ -209,7 +215,7 @@ def filter_for_lookup(
209
215
return (
210
216
data ["filter_class" ],
211
217
{
212
- ** cls .enum_extra (field ),
218
+ ** FilterSet .enum_extra (field ),
213
219
** data .get ("extra" , lambda f : {})(field ),
214
220
},
215
221
)
0 commit comments