Skip to content

Commit

Permalink
Merge pull request carltongibson#188 from qrilka/patch-1
Browse files Browse the repository at this point in the history
TypedChoiceFilter
  • Loading branch information
Carlton Gibson committed Nov 25, 2014
2 parents c6d702d + b6fde04 commit 2145283
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions django_filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

__all__ = [
'Filter', 'CharFilter', 'BooleanFilter', 'ChoiceFilter',
'MultipleChoiceFilter', 'DateFilter', 'DateTimeFilter', 'TimeFilter',
'ModelChoiceFilter', 'ModelMultipleChoiceFilter', 'NumberFilter',
'RangeFilter', 'DateRangeFilter', 'AllValuesFilter', 'MethodFilter'
'TypedChoiceFilter', 'MultipleChoiceFilter', 'DateFilter',
'DateTimeFilter', 'TimeFilter', 'ModelChoiceFilter',
'ModelMultipleChoiceFilter', 'NumberFilter', 'RangeFilter',
'DateRangeFilter', 'AllValuesFilter', 'MethodFilter'
]


Expand Down Expand Up @@ -97,6 +98,10 @@ class ChoiceFilter(Filter):
field_class = forms.ChoiceField


class TypedChoiceFilter(Filter):
field_class = forms.TypedChoiceField


class MultipleChoiceFilter(Filter):
"""
This filter preforms an OR query on the selected options.
Expand Down
20 changes: 20 additions & 0 deletions docs/ref/filters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ This filter matches a boolean, either ``True`` or ``False``, used with
This filter matches an item of any type by choices, used with any field that
has ``choices``.

``TypedChoiceFilter``
~~~~~~~~~~~~~~~~

The same as ``ChoiceFilter`` with the added possibility to convert value to
match against. This could be done by using `coerce` parameter.
An example use-case is limiting boolean choices to match against so only
some predefined strings could be used as input of a boolean filter:

.. sourcecode:: python
import django_filters
from distutils.util import strtobool

BOOLEAN_CHOICES = (('false', 'False'), ('true', 'True'),)

class YourFilterSet(django_filters.FilterSet):
...
flag = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES,
coerce=strtobool)


``MultipleChoiceFilter``
~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit 2145283

Please sign in to comment.