You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/filtering.md
+26-3Lines changed: 26 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -95,9 +95,9 @@ You can also set the filter backends on a per-view, or per-viewset basis,
95
95
using the `GenericAPIView` class based views.
96
96
97
97
from django.contrib.auth.models import User
98
-
from myapp.serializers import UserSerializer
98
+
from myapp.serializers import UserSerializer
99
99
from rest_framework import filters
100
-
from rest_framework import generics
100
+
from rest_framework import generics
101
101
102
102
class UserListView(generics.ListAPIView):
103
103
queryset = User.objects.all()
@@ -141,6 +141,13 @@ To use REST framework's `DjangoFilterBackend`, first install `django-filter`.
141
141
142
142
pip install django-filter
143
143
144
+
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.
145
+
146
+
pip install django-crispy-forms
147
+
148
+
With crispy forms installed, the browsable API will present a filtering control for `DjangoFilterBackend`, like so:
@@ -237,6 +244,10 @@ For more details on using filter sets see the [django-filter documentation][djan
237
244
238
245
The `SearchFilter` class supports simple single query parameter based searching, and is based on the [Django admin's search functionality][search-django-admin].
239
246
247
+
When in use, the browsable API will include a `SearchFilter` control:
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`.
241
252
242
253
class UserListView(generics.ListAPIView):
@@ -274,7 +285,11 @@ For more details, see the [Django documentation][search-django-admin].
274
285
275
286
## OrderingFilter
276
287
277
-
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.
288
+
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.
278
293
279
294
For example, to order users by username:
280
295
@@ -389,6 +404,14 @@ For example, you might need to restrict users to only being able to see objects
389
404
390
405
We could achieve the same behavior by overriding `get_queryset()` on the views, but using a filter backend allows you to more easily add this restriction to multiple views, or to apply it across the entire API.
391
406
407
+
## Customizing the interface
408
+
409
+
Generic filters may also present an interface in the browsable API. To do so you should implement a `to_html()` method which returns a rendered HTML representation of the filter. This method should have the following signature:
410
+
411
+
`to_html(self, request, queryset, view)`
412
+
413
+
The method should return a rendered HTML string.
414
+
392
415
# Third party packages
393
416
394
417
The following third party packages provide additional filter implementations.
0 commit comments