rest_framework_tricks.filters package

Submodules

rest_framework_tricks.filters.ordering module

Ordering filter.

class rest_framework_tricks.filters.ordering.OrderingFilter[source]

Bases: rest_framework.filters.OrderingFilter

Ordering filter improved.

Example:

>>> from rest_framework_tricks.filters import OrderingFilter
>>>
>>> class BooksViewSet(mixins.RetrieveModelMixin,
>>>                    mixins.ListModelMixin,
>>>                    viewsets.GenericViewSet):
>>>
>>>     serializer_class = BookSerializer
>>>     filter_backends = (
>>>         OrderingFilter,
>>>     )
>>>     ordering_fields = {
>>>         'email': 'user__email',
>>>         'full_name': 'user__first_name',
>>>         'last_login': 'user__last_login',
>>>         'is_active': 'user__is_active',
>>>     }

Then it can be used in a view set as follows:

GET /books/api/proxy-books/?ordering=email

get_ordering(request, queryset, view)[source]

Get ordering.

Important: list returned in this method is used directly in the filter_queryset method like:

>>> queryset.order_by(*ordering)

Ordering is set by a comma delimited ?ordering=… query parameter.

The ordering query parameter can be overridden by setting the ordering_param value on the OrderingFilter or by specifying an ORDERING_PARAM value in the API settings.

get_valid_fields(queryset, view, context=None)[source]

Done.

Parameters
  • queryset

  • view

  • context

Returns

Module contents

Filters.

class rest_framework_tricks.filters.OrderingFilter[source]

Bases: rest_framework.filters.OrderingFilter

Ordering filter improved.

Example:

>>> from rest_framework_tricks.filters import OrderingFilter
>>>
>>> class BooksViewSet(mixins.RetrieveModelMixin,
>>>                    mixins.ListModelMixin,
>>>                    viewsets.GenericViewSet):
>>>
>>>     serializer_class = BookSerializer
>>>     filter_backends = (
>>>         OrderingFilter,
>>>     )
>>>     ordering_fields = {
>>>         'email': 'user__email',
>>>         'full_name': 'user__first_name',
>>>         'last_login': 'user__last_login',
>>>         'is_active': 'user__is_active',
>>>     }

Then it can be used in a view set as follows:

GET /books/api/proxy-books/?ordering=email

get_ordering(request, queryset, view)[source]

Get ordering.

Important: list returned in this method is used directly in the filter_queryset method like:

>>> queryset.order_by(*ordering)

Ordering is set by a comma delimited ?ordering=… query parameter.

The ordering query parameter can be overridden by setting the ordering_param value on the OrderingFilter or by specifying an ORDERING_PARAM value in the API settings.

get_valid_fields(queryset, view, context=None)[source]

Done.

Parameters
  • queryset

  • view

  • context

Returns