BaseFilterBackend
Base class from which all filter backend classes should inherit.Methods
filter_queryset(request, queryset, view)
Return a filtered queryset.
The request instance
The queryset to filter
The view instance
get_schema_operation_parameters(view)
Return a list of OpenAPI operation parameters for the filter.
The view instance
SearchFilter
Provides simple search behavior based on a single query parameter.Attributes
The URL query parameter used for the search. Default is
'search'Template for rendering search controls. Default is
'rest_framework/filters/search.html'Mapping of search prefixes to Django lookup types:
^-istartswith(starts with)=-iexact(exact match)@-search(full-text search)$-iregex(regex)
View Attributes
Set these on your view to configure search:List of field names to search. Prefix with lookup operators to change behavior:
'^username'- Starts with search'=email'- Exact match'@description'- Full-text search'$content'- Regex search'username'- Default (contains search)
Methods
get_search_fields(view, request)
Return the list of search fields to use.
get_search_terms(request)
Extract search terms from the request query parameters.
construct_search(field_name, queryset)
Construct the full ORM lookup for a given field name.
must_call_distinct(queryset, search_fields)
Return True if distinct() should be used to query the given lookups.
Example
OrderingFilter
Provides simple ordering of results based on query parameters.Attributes
The URL query parameter used for ordering. Default is
'ordering'List of field names that are allowed for ordering. Set to
'__all__' to allow any field. Default is NoneTemplate for rendering ordering controls. Default is
'rest_framework/filters/ordering.html'View Attributes
Whitelist of field names that can be used for ordering. Special value
'__all__' allows ordering by any fieldDefault ordering to use if not specified by the client
Methods
get_ordering(request, queryset, view)
Return the ordering fields to use, or None.
get_default_ordering(view)
Return the default ordering for the view.
get_valid_fields(queryset, view, context=None)
Return a list of (field_name, field_label) tuples for valid ordering fields.
remove_invalid_fields(queryset, fields, view, request)
Remove any invalid fields from the ordering list.
Example
DjangoFilterBackend Integration
DRF provides integration with thedjango-filter library for more complex filtering:
Custom Filter Backends
Create custom filter backends by subclassingBaseFilterBackend:
