@@ -79,23 +79,22 @@ We can override `.get_queryset()` to deal with URLs such as `http://example.com/
...
@@ -79,23 +79,22 @@ We can override `.get_queryset()` to deal with URLs such as `http://example.com/
As well as being able to override the default queryset, REST framework also includes support for generic filtering backends that allow you to easily construct complex filters that can be specified by the client using query parameters.
As well as being able to override the default queryset, REST framework also includes support for generic filtering backends that allow you to easily construct complex filters that can be specified by the client using query parameters.
REST framework supports pluggable backends to implement filtering, and provides an implementation which uses the [django-filter] package.
## DjangoFilterBackend
To use REST framework's filtering backend, first install `django-filter`.
To use REST framework's `DjangoFilterBackend`, first install `django-filter`.
pip install django-filter
pip install django-filter
You must also set the filter backend to `DjangoFilterBackend` in your settings:
You must also set the filter backend to `DjangoFilterBackend` in your settings:
If all you need is simple equality-based filtering, you can set a `filter_fields` attribute on the view, or viewset,
If all you need is simple equality-based filtering, you can set a `filter_fields` attribute on the view, or viewset, listing the set of fields you wish to filter against.
listing the set of fields you wish to filter against.
class ProductList(generics.ListAPIView):
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
queryset = Product.objects.all()
...
@@ -106,7 +105,7 @@ This will automatically create a `FilterSet` class for the given fields, and wil
...
@@ -106,7 +105,7 @@ This will automatically create a `FilterSet` class for the given fields, and wil
For more advanced filtering requirements you can specify a `FilterSet` class that should be used by the view. For example:
For more advanced filtering requirements you can specify a `FilterSet` class that should be used by the view. For example:
...
@@ -132,13 +131,13 @@ For more details on using filter sets see the [django-filter documentation][djan
...
@@ -132,13 +131,13 @@ For more details on using filter sets see the [django-filter documentation][djan
**Hints & Tips**
**Hints & Tips**
* By default filtering is not enabled. If you want to use `DjangoFilterBackend` remember to make sure it is installed by using the `'FILTER_BACKEND'` setting.
* By default filtering is not enabled. If you want to use `DjangoFilterBackend` remember to make sure it is installed by using the `'DEFAULT_FILTER_BACKENDS'` setting.
* When using boolean fields, you should use the values `True` and `False` in the URL query parameters, rather than `0`, `1`, `true` or `false`. (The allowed boolean values are currently hardwired in Django's [NullBooleanSelect implementation][nullbooleanselect].)
* When using boolean fields, you should use the values `True` and `False` in the URL query parameters, rather than `0`, `1`, `true` or `false`. (The allowed boolean values are currently hardwired in Django's [NullBooleanSelect implementation][nullbooleanselect].)
*`django-filter` supports filtering across relationships, using Django's double-underscore syntax.
*`django-filter` supports filtering across relationships, using Django's double-underscore syntax.
---
---
### Filtering and object lookups
## Filtering and object lookups
Note that if a filter backend is configured for a view, then as well as being used to filter list views, it will also be used to filter the querysets used for returning a single object.
Note that if a filter backend is configured for a view, then as well as being used to filter list views, it will also be used to filter the querysets used for returning a single object.
...
@@ -170,12 +169,12 @@ You can also provide your own generic filtering backend, or write an installable
...
@@ -170,12 +169,12 @@ You can also provide your own generic filtering backend, or write an installable
To do so override `BaseFilterBackend`, and override the `.filter_queryset(self, request, queryset, view)` method. The method should return a new, filtered queryset.
To do so override `BaseFilterBackend`, and override the `.filter_queryset(self, request, queryset, view)` method. The method should return a new, filtered queryset.
To install the filter backend, set the `'FILTER_BACKEND'` key in your `'REST_FRAMEWORK'` setting, using the dotted import path of the filter backend class.
To install the filter backend, set the `'DEFAULT_FILTER_BACKENDS'` key in your `'REST_FRAMEWORK'` setting, using the dotted import path of the filter backend class.
@@ -77,7 +77,7 @@ The following attibutes are used to control pagination when used with list views
...
@@ -77,7 +77,7 @@ The following attibutes are used to control pagination when used with list views
**Filtering**:
**Filtering**:
*`filter_backend` - The filter backend class that should be used for filtering the queryset. Defaults to the same value as the `FILTER_BACKEND` setting.
*`filter_backends` - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the `DEFAULT_FILTER_BACKENDS` setting.
The `FILTER_BACKEND` setting has moved to pending deprecation, in favor of a `DEFAULT_FILTER_BACKENDS` setting that takes a *list* of filter backend classes, instead of a single filter backend class.
The generic view `filter_backend` attribute has also been moved to pending deprecation in favor of a `filter_backends` setting.
Being able to specify multiple filters will allow for more flexible, powerful behavior. New filter classes to handle searching and ordering of results are planned to be released shortly.