Skip to content

Commit

Permalink
Update docs on filter_overrides usage
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson committed Nov 18, 2016
1 parent a776d52 commit e2c09cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.PHONY: deps, test
.PHONY: deps, test, clean

deps:
pip install -r ./requirements/test.txt

test:
./runtests.py
./runtests.py

clean:
rm -r build dist django_filter.egg-info

4 changes: 2 additions & 2 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ def filter_for_field(cls, f, name, lookup_expr='exact'):

assert filter_class is not None, (
"%s resolved field '%s' with '%s' lookup to an unrecognized field "
"type %s. Try adding an override to 'filter_overrides'. See: "
"https://django-filter.readthedocs.io/en/latest/usage.html#overriding-default-filters"
"type %s. Try adding an override to 'Meta.filter_overrides'. See: "
"https://django-filter.readthedocs.io/en/latest/ref/filterset.html#customise-filter-generation-with-filter-overrides"
) % (cls.__name__, name, lookup_expr, f.__class__.__name__)

return filter_class(**default)
Expand Down
30 changes: 15 additions & 15 deletions docs/guide/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,30 @@ Overriding default filters

Like ``django.contrib.admin.ModelAdmin``, it is possible to override
default filters for all the models fields of the same kind using
``filter_overrides``::
``filter_overrides`` on the ``Meta`` class::

class ProductFilter(django_filters.FilterSet):
filter_overrides = {
models.CharField: {
'filter_class': django_filters.CharFilter,
'extra': lambda f: {
'lookup_expr': 'icontains',
},
},
models.BooleanField: {
'filter_class': django_filters.BooleanFilter,
'extra': lambda f: {
'widget': forms.CheckboxInput,
},
},
}

class Meta:
model = Product
fields = {
'name': ['exact'],
'release_date': ['isnull'],
}
filter_overrides = {
models.CharField: {
'filter_class': django_filters.CharFilter,
'extra': lambda f: {
'lookup_expr': 'icontains',
},
},
models.BooleanField: {
'filter_class': django_filters.BooleanFilter,
'extra': lambda f: {
'widget': forms.CheckboxInput,
},
},
}


Request-based filtering
Expand Down
32 changes: 31 additions & 1 deletion docs/ref/filterset.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Meta options
- :ref:`exclude <exclude>`
- :ref:`form <form>`
- :ref:`together <together>`
- filter_overrides
- :ref:`filter_overrides <filter_overrides>`
- :ref:`strict <strict>`


Expand Down Expand Up @@ -120,6 +120,36 @@ field set must either be all or none present in the request for
fields = ['price', 'release_date', 'rating']
together = ['rating', 'price']


.. _filter_overrides:

Customise filter generation with ``filter_overrides``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The inner ``Meta`` class also takes an optional ``filter_overrides`` argument.
This is a map of model fields to filter classes with options::

class ProductFilter(django_filters.FilterSet):

class Meta:
model = Product
fields = ['name', 'release_date']
filter_overrides = {
models.CharField: {
'filter_class': django_filters.CharFilter,
'extra': lambda f: {
'lookup_expr': 'icontains',
},
},
models.BooleanField: {
'filter_class': django_filters.BooleanFilter,
'extra': lambda f: {
'widget': forms.CheckboxInput,
},
},
}


.. _strict:

``strict``
Expand Down

0 comments on commit e2c09cb

Please sign in to comment.