Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Docs on the filter HTML interface
  • Loading branch information
lovelydinosaur committed Oct 22, 2015
commit 0c6d46729c711c5df5e587eceacaaab753b05b01
23 changes: 20 additions & 3 deletions docs/api-guide/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ You can also set the filter backends on a per-view, or per-viewset basis,
using the `GenericAPIView` class based views.

from django.contrib.auth.models import User
from myapp.serializers import UserSerializer
from myapp.serializers import UserSerializer
from rest_framework import filters
from rest_framework import generics
from rest_framework import generics

class UserListView(generics.ListAPIView):
queryset = User.objects.all()
Expand Down Expand Up @@ -141,6 +141,13 @@ To use REST framework's `DjangoFilterBackend`, first install `django-filter`.

pip install django-filter

If you are using the browsable API or admin API you may also want to install `crispy-forms`, which will enhance the presentation of the filter forms in HTML views, by allowing them to render Bootstrap 3 HTML.

pip install django-crispy-forms

With crispy forms installed, the browsable API will present a filtering control for `DjangoFilterBackend`, like so:

![Django Filter](../../docs/img/django-filter.png)

#### Specifying filter fields

Expand Down Expand Up @@ -237,6 +244,10 @@ For more details on using filter sets see the [django-filter documentation][djan

The `SearchFilter` class supports simple single query parameter based searching, and is based on the [Django admin's search functionality][search-django-admin].

When in use, the browsable API will include a `SearchFilter` control:

![Search Filter](../../docs/img/search-filter.png)

The `SearchFilter` class will only be applied if the view has a `search_fields` attribute set. The `search_fields` attribute should be a list of names of text type fields on the model, such as `CharField` or `TextField`.

class UserListView(generics.ListAPIView):
Expand Down Expand Up @@ -274,7 +285,11 @@ For more details, see the [Django documentation][search-django-admin].

## OrderingFilter

The `OrderingFilter` class supports simple query parameter controlled ordering of results. By default, the query parameter is named `'ordering'`, but this may by overridden with the `ORDERING_PARAM` setting.
The `OrderingFilter` class supports simple query parameter controlled ordering of results.

![Ordering Filter](../../docs/img/ordering-filter.png)

By default, the query parameter is named `'ordering'`, but this may by overridden with the `ORDERING_PARAM` setting.

For example, to order users by username:

Expand Down Expand Up @@ -395,6 +410,8 @@ Generic filters may also present an interface in the browsable API. To do so you

`to_html(self, request, queryset, view)`

The method should return a rendered HTML string.

# Third party packages

The following third party packages provide additional filter implementations.
Expand Down
Binary file added docs/img/django-filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/ordering-filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/search-filter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ def get_filter_form(self, data, view, request):

# Infer if this is a list view or not.
paginator = getattr(view, 'paginator', None)
if (paginator is not None and data is not None):
if isinstance(data, list):
pass
elif (paginator is not None and data is not None):
try:
paginator.get_results(data)
except (TypeError, KeyError):
Expand Down