rest_framework_json_api.filters module

class rest_framework_json_api.filters.OrderingFilter

Bases: OrderingFilter

A backend filter that implements https://jsonapi.org/format/#fetching-sorting and raises a 400 error if any sort field is invalid.

If you prefer not to report 400 errors for invalid sort fields, just use rest_framework.filters.OrderingFilter with ordering_param = “sort”

Also supports undo of field name formatting (See JSON_API_FORMAT_FIELD_NAMES in docs/usage.md)

ordering_param = 'sort'

override rest_framework.filters.OrderingFilter.ordering_param with JSON:API-compliant query parameter name.

ordering_description = '[list of fields to sort by](https://jsonapi.org/format/#fetching-sorting)'
remove_invalid_fields(queryset, fields, view, request)

Extend rest_framework.filters.OrderingFilter.remove_invalid_fields() to validate that all provided sort fields exist (as contrasted with the super’s behavior which is to silently remove invalid fields).

Raises:

ValidationError – if a sort field is invalid.

class rest_framework_json_api.filters.QueryParameterValidationFilter

Bases: BaseFilterBackend

A backend filter that performs strict validation of query parameters for JSON:API spec conformance and raises a 400 error if non-conforming usage is found.

If you want to add some additional non-standard query parameters, override query_regex adding the new parameters. Make sure to comply with the rules at https://jsonapi.org/format/#query-parameters.

query_regex = re.compile('^(sort|include)$|^(?P<type>filter|fields|page)(\\[[\\w\\.\\-]+\\])?$')

compiled regex that matches the allowed https://jsonapi.org/format/#query-parameters: sort and include stand alone; filter, fields, and page have []’s

validate_query_params(request)

Validate that query params are in the list of valid query keywords in query_regex

Raises:

ValidationError – if not.

filter_queryset(request, queryset, view)

Overrides BaseFilterBackend.filter_queryset() by first validating the query params with validate_query_params()