Skip to main content

Django Rest Filter

Project description

dj-rest-filters

dj-rest-filters is a Django application allowing users to declaratively add dynamic QuerySet filtering from URL parameters. Its uses Django Rest Framework serializers in backend so it provides same syntax as of serializers. You can validate you query parameter in same way as you validate in your serializer and it also provides filtering mechanism against custom query parameters.

Build codecov Docs

Installation

pip install dj-rest-filters

Then add 'djfilters' to your INSTALLED_APPS.

Usage

Filtering

from rest_framework import generics
from djfilters.backend import DjFilterBackend


class TodoView(generics.GenericAPIView):
    serializer_class = ...
    filter_class = TodoFilter
    filter_backends = [DjFilterBackend]
    queryset = Todo.objects.all()

Validation Only

When queryset is declared, filter will be used to filter that queryset. But when there is not any queryset, filter will just be used to validate the provided query parameters.

from rest_framework import generics

class TodoView(generics.GenericAPIView):
    serializer_class = ...
    filter_class = MyFilter

Filter Context

Just like serializer context, context can be passed to filter by using get_filter_context function like this.

from rest_framework import generics

class TodoView(generics.GenericAPIView):
    ....

    def get_filter_context(self):
        context = {'extra_data': 'some_extra_data'}
        return context

Accessing Validated Query Params

After query param validation, validated parameters can be accessed using request.cleaned_args.

For detailed documentation, click here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-rest-filters-1.0.3.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

dj_rest_filters-1.0.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file dj-rest-filters-1.0.3.tar.gz.

File metadata

  • Download URL: dj-rest-filters-1.0.3.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for dj-rest-filters-1.0.3.tar.gz
Algorithm Hash digest
SHA256 d2c74ea8e89e681199506371f2b695749948a80657835ce74be94df79d586541
MD5 b68da761a0246820c0b15919e6f128b9
BLAKE2b-256 c038a2374c5b538add9552fd842462e7136b8eb9294fc19df57df9c72abc22b0

See more details on using hashes here.

File details

Details for the file dj_rest_filters-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for dj_rest_filters-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5e9d9a66e4febd09e713747a5ab4721212e2060f8395c6594dc8e5561d3bccfc
MD5 9c10f562a58fa62a17f22097a5793e5e
BLAKE2b-256 711c8cd715058de0a859ea4f4ad5417a6187eb99089803b97b119cf9451266e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page