Skip to main content

FilterMapBackend

Test Status

Table Of Content

Introduction

FilterBackend which takes mapping of query params to field name.

It takes the query_param to filter map and enables filter option in list view.

Installation

Install drf-filtermapbackend using

pip install drf-filtermapbackend

Then include filter_map in your installed apps

INSTALLED_APPS = [
    ...,
    'rest_framework',
    'filter_map',
    ...
]

Usage

You can use FilterMapBackend by adding it to your filter backends and setting filter_map attribute. For example

from rest_framework.viewsets import ModelViewSet
from filter_map.backends import FilterMapBackend

class ProfileViewSet(ModelViewSet):
    """
    Consider Profile Model has user FK,
    """
    queryset = ...
    serializer_class = ...
    filter_backends = (FilterMapBackend,)
    filter_map = {
        # plain map
        'first_name': 'user__first_name',
        
        # used with lte operator
        'joined_before': 'date_joined__date__lte',
        
        # also supports separate field name and operator 
        'last_name': ('user__last_name', 'iexact'),
    }
    

You can also define get_filter_map method to return the filter map. This will allow you to change filter_map in runtime. Here's an example

from rest_framework.viewsets import ModelViewSet
from filter_map.backends import FilterMapBackend

class ProfileViewSet(ModelViewSet):
    """
    Consider Profile Model has user FK,
    """
    queryset = ...
    serializer_class = ...
    filter_backends = (FilterMapBackend,)
    
    def get_filter_map(self):
        # Disable joined_before filter for non staff users
        if self.request.user.is_authenticated and self.request.user.is_staff:
            return {
                'first_name': 'user__first_name',
                'joined_before': 'date_joined__date__lte',
                'last_name': ('user__last_name', 'iexact'),
            }
        else:
            return {
                'first_name': 'user__first_name',
                'last_name': ('user__last_name', 'iexact'),
            }
            

Metadata

Release files for drf-filtermapbackend 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-filtermapbackend 0.0.1
File Size Uploaded Provenance
drf-filtermapbackend-0.0.1.tar.gz 17.7 kB Without provenance Details

Release files / drf-filtermapbackend-0.0.1.tar.gz

Download URL drf-filtermapbackend-0.0.1.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9eab5bbb56676593effaf1ee8028b36a679f1f5908afd9d4b2a698a6b37f0269
BLAKE2b-256 checksum
How to use checksums
5b5145a2c9530e01fcae65576e47d877278aea8d797a83b254dbb2a250401bf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.6

Security for drf-filtermapbackend 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page