Added `livehtml` doc loader, and updated docs on `MethodFilter`
This commit is contained in:
parent
d926be358b
commit
6a8900b510
|
@ -151,3 +151,5 @@ doctest:
|
|||
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
|
||||
@echo "Testing of doctests in the sources finished, look at the " \
|
||||
"results in $(BUILDDIR)/doctest/output.txt."
|
||||
livehtml:
|
||||
sphinx-autobuild -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
|
||||
|
|
|
@ -115,6 +115,72 @@ default filters for all the models fields of the same kind using
|
|||
fields = ['name']
|
||||
|
||||
|
||||
MethodFilter
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
If you want fine control over each individual filter attribute, you can use
|
||||
the ``MethodFilter`` filter.
|
||||
|
||||
By passing in the name of a custom defined filter function as an ``action``,
|
||||
the filter attribute gets linked to the custom filter function.
|
||||
Here is an example of overriding the filter function of the
|
||||
filter attribute ``username``
|
||||
::
|
||||
|
||||
class F(django_filters.FilterSet):
|
||||
username = MethodFilter(action='my_custom_filter')
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username']
|
||||
|
||||
def my_custom_filter(self, queryset, value):
|
||||
return queryset.filter(
|
||||
username=value
|
||||
)
|
||||
|
||||
|
||||
The filter function can also be defined outside of the filter class scope.
|
||||
Though you would need to pass in the actual function value, not it's name.
|
||||
::
|
||||
|
||||
def my_custom_filter(queryset, value):
|
||||
return queryset.filter(
|
||||
username=value
|
||||
)
|
||||
|
||||
class F(django_filters.FilterSet):
|
||||
# Notice: In this case, action accepts a func, not a string
|
||||
username = MethodFilter(action=filter_username)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username']
|
||||
|
||||
|
||||
Lastly, when using a ``MethodFilter``, there is no need to define an action.
|
||||
You may simply do the following and ``filter_username`` will be auto-detected
|
||||
and used. ::
|
||||
|
||||
class F(FilterSet):
|
||||
username = MethodFilter()
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username']
|
||||
|
||||
def filter_username(self, queryset, value):
|
||||
return queryset.filter(
|
||||
username__contains='ke'
|
||||
)
|
||||
|
||||
Under the hood, if ``action`` not is defined, ``django_filter``
|
||||
searches for a class method with a name that follows the pattern
|
||||
``filter_{{ATTRIBUTE_NAME}}``. For example, if the attribute name is
|
||||
``email``, then the filter class will be scanned for the filter function
|
||||
``filter_email``. If no action is provided, and no filter class
|
||||
function is found, then the filter attribute will be left unfiltered.
|
||||
|
||||
|
||||
The view
|
||||
--------
|
||||
|
||||
|
|
|
@ -2,3 +2,4 @@ Django
|
|||
django-discover-runner
|
||||
mock
|
||||
coverage
|
||||
sphinx-autobuild
|
||||
|
|
Loading…
Reference in New Issue