mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
Docs on the filter HTML interface
This commit is contained in:
parent
0d1637d666
commit
0c6d46729c
|
@ -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.
|
using the `GenericAPIView` class based views.
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
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 filters
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
|
|
||||||
class UserListView(generics.ListAPIView):
|
class UserListView(generics.ListAPIView):
|
||||||
queryset = User.objects.all()
|
queryset = User.objects.all()
|
||||||
|
@ -141,6 +141,13 @@ To use REST framework's `DjangoFilterBackend`, first install `django-filter`.
|
||||||
|
|
||||||
pip 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
|
#### Specifying filter fields
|
||||||
|
|
||||||
|
@ -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].
|
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`.
|
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):
|
class UserListView(generics.ListAPIView):
|
||||||
|
@ -274,7 +285,11 @@ For more details, see the [Django documentation][search-django-admin].
|
||||||
|
|
||||||
## OrderingFilter
|
## 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:
|
For example, to order users by username:
|
||||||
|
|
||||||
|
@ -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)`
|
`to_html(self, request, queryset, view)`
|
||||||
|
|
||||||
|
The method should return a rendered HTML string.
|
||||||
|
|
||||||
# Third party packages
|
# Third party packages
|
||||||
|
|
||||||
The following third party packages provide additional filter implementations.
|
The following third party packages provide additional filter implementations.
|
||||||
|
|
BIN
docs/img/django-filter.png
Normal file
BIN
docs/img/django-filter.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
BIN
docs/img/ordering-filter.png
Normal file
BIN
docs/img/ordering-filter.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
BIN
docs/img/search-filter.png
Normal file
BIN
docs/img/search-filter.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.9 KiB |
|
@ -591,7 +591,9 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
|
|
||||||
# Infer if this is a list view or not.
|
# Infer if this is a list view or not.
|
||||||
paginator = getattr(view, 'paginator', None)
|
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:
|
try:
|
||||||
paginator.get_results(data)
|
paginator.get_results(data)
|
||||||
except (TypeError, KeyError):
|
except (TypeError, KeyError):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user