FilterSet Reference

class rest_filters.filtersets.FilterSet
request

Django REST framework Request object.

view

View instance for this request.

get_combinator(group: str, entries: Entries) Callable[..., Any]

Resolve the logical operator for the given group.

Parameters:
  • group – Name of the group that is currently being resolved.

  • entries – Query parameters belonging to this group, with their corresponding Entry.

Returns:

A callable that acts like a logical operator, such as operator.or_ and operator.and_

get_group_entry(group: str, entries: dict[str, Entry]) Entry

Resolve Entry for the given group.

Parameters:
  • group – Name of the group that is currently being resolved.

  • entries – Query parameters belonging to this group, with their corresponding Entry.

get_queryset(queryset: QuerySet[_MT_co], values: dict[str, Any]) QuerySet[_MT_co]

Returns the final QuerySet object. At this point, all the filters are applied. Override this method to perform operations on QuerySet that are otherwise not possible, such as order_by() and distinct() calls.

Parameters:
  • queryset – Filtered QuerySet object.

  • values – Parsed query parameters.

get_fields() dict[str, Filter]

Resolve filters that are going to be used in this FilterSet. You may override this method to dynamically add filters.

Danger

Make sure additional Filter instances are initialized inside this method, using global variables will lead to dangling references.

get_default(param: str, default: Any) Any

Dynamically determine the default value for the given param.

Parameters:
  • param – Parameter name.

  • default – Default value that is otherwise going to be used.

Returns:

Default value.

get_serializer(param: str, serializer: Field | None) Field

Dynamically resolve the serializer field for the given param.

Parameters:
  • param – Parameter name.

  • serializer – Serializer field that is otherwise going to be used.

Returns:

Serializer field.

get_serializer_context(param: str) dict[str, Any]

Get serializer context for the given param. By default, this will use view.get_serializer_context(). The context will also include this FilterSet instance.

Parameters:

param – Parameter name.

Returns:

Context dictionary.

run_validation(value: str | _Empty, serializer: AnyField, param: str) Any

Run validation for the given param.

Parameters:
  • value – Value provided by the user. This will be empty if the parameter is missing.

  • serializer – Serializer field that is going to be used for validation.

  • param – Parameter name.

Returns:

Parsed query parameter value.

get_constraints() Sequence[Constraint]

Resolve constraint objects that are going to be used in this FilterSet. You may override this method to dynamically add constraints.

Danger

Make sure additional Constraint instances are initialized inside this method, using global variables will lead to dangling references.

handle_unknown_parameters(unknown: list[str], known: list[str]) dict[str, Any]

Creates error messages for unknown parameters.

Parameters:
  • unknown – Unknown parameters the user supplied.

  • known – Known parameters.

Returns:

An error dictionary.

handle_errors(errordict: dict[str, Any]) None

Raises ValidationError for given errors. You may override this method to change the error format.