url_filter.integrations.drf module

class url_filter.integrations.drf.DjangoFilterBackend[source]

Bases: rest_framework.filters.BaseFilterBackend

DRF filter backend which integrates with django-url-filter

This integration backend can be specified in global DRF settings:

# settings.py

Alternatively filter backend can be specified per view/viewset bases:

class MyViewSet(ModelViewSet):
    queryset = MyModel.objects.all()
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['field1', 'field2']

The following attributes can be specified on the view:

  • filter_class - explicit filter (FilterSet to be specific) class which should be used for filtering. When this attribute is supplied, this filterset will be used and all other attributes described below will are ignored.

  • filter_fields - list of strings which should be names of fields which should be included in the generated FilterSet. This is equivalent:

    class MyFilterSet(ModelFilterSet):
        class Meta(object):
            model = MyModel
            fields = ['fields1', ...]
  • filter_class_meta_kwargs - additional kwargs which should be passed in Meta for the generated FilterSet.

  • filter_class_default - base class to use while creating new FilterSet. This is primarily useful when using non-Django data-sources. By default default_filter_set is used.


alias of url_filter.filtersets.django.ModelFilterSet

filter_queryset(request, queryset, view)[source]

Main method for filtering query object

  • request (HttpRequest) – Request object from the view
  • queryset – Query object for filtering
  • view (View) – View where this filter backend is being used

Filtered query object if filtering class was determined by get_filter_class(). If not given queryset is returned.

Return type:


get_filter_class(view, queryset=None)[source]

Get filter class which will be used for filtering.

  • view (View) – DRF view/viewset where this filter backend is being used. Please refer to DjangoFilterBackend documentation for list of attributes which can be supplied in view to customize how filterset will be determined.
  • queryset – Query object for filtering

  • FilterSetFilterSet class either directly specified in the view or dynamically constructed for the queryset model.
  • None – When appropriate FilterSet cannot be determined for filtering

get_filter_context(request, view)[source]

Get context to be passed to FilterSet during initialization

  • request (HttpRequest) – Request object from the view
  • view (View) – View where this filter backend is being used

Context to be passed to FilterSet

Return type:



alias of url_filter.integrations.drf.DjangoFilterBackend