All Projects → manjitkumar → Drf Url Filters

manjitkumar / Drf Url Filters

Licence: mit
A django app to apply filters on drf querysets using query params with validations using voluptuous.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Drf Url Filters

Django Admin Numeric Filter
Numeric filters for Django admin
Stars: ✭ 46 (-69.33%)
Mutual labels:  django, filters
Djangocms Installer
Console wizard to bootstrap django CMS projects
Stars: ✭ 148 (-1.33%)
Mutual labels:  django
Django Plus.vim
🎸 Improvements to the handling of Django related files in Vim
Stars: ✭ 145 (-3.33%)
Mutual labels:  django
Bento
[DEPRECATED] Find Python web-app bugs delightfully fast, without changing your workflow. 🍱
Stars: ✭ 147 (-2%)
Mutual labels:  django
Kubernetes Django
Scalable and resilient Django with Kubernetes.
Stars: ✭ 145 (-3.33%)
Mutual labels:  django
Django Gamification
The missing Django Gamification Package
Stars: ✭ 147 (-2%)
Mutual labels:  django
Openwisp Users
Implementation of user management and multi-tenancy for OpenWISP
Stars: ✭ 145 (-3.33%)
Mutual labels:  django
React Django
Simple setup for a React-Django web app.
Stars: ✭ 149 (-0.67%)
Mutual labels:  django
Alize
Visualize Your Github Profile
Stars: ✭ 148 (-1.33%)
Mutual labels:  django
Django Sql Explorer
Easily share data across your company via SQL queries. From Grove Collab.
Stars: ✭ 1,958 (+1205.33%)
Mutual labels:  django
Django Mail Templated
Send emails using Django template system
Stars: ✭ 146 (-2.67%)
Mutual labels:  django
Sciblog
A blog made with django designed like a scientific paper written in Latex.
Stars: ✭ 145 (-3.33%)
Mutual labels:  django
Django Pgcrypto Fields
Transparent field level encryption for Django using the pgcrypto postgresql extension.
Stars: ✭ 147 (-2%)
Mutual labels:  django
Twitterdatamining
Twitter数据挖掘及其可视化
Stars: ✭ 145 (-3.33%)
Mutual labels:  django
Book recommend
基于协同过滤的书籍推荐系统
Stars: ✭ 149 (-0.67%)
Mutual labels:  django
Slacklibrary
图书馆借还系统,一个Django写的练手项目
Stars: ✭ 144 (-4%)
Mutual labels:  django
Django Easy Pjax
Easy PJAX for Django
Stars: ✭ 146 (-2.67%)
Mutual labels:  django
Django Pg Timepart
A Django extension that supports PostgreSQL 11 time ranges and list partitioning.
Stars: ✭ 147 (-2%)
Mutual labels:  django
Appstore
🏪 App Store for Nextcloud
Stars: ✭ 149 (-0.67%)
Mutual labels:  django
Django Celery Docker Example
Example Docker setup for a Django app behind an Nginx proxy with Celery workers
Stars: ✭ 149 (-0.67%)
Mutual labels:  django

drf-url-filters

Help Contribute to Open Source

drf-url-filters is a simple django app to apply filters on drf modelviewset's queryset in a clean, simple and configurable way. It also supports validations on incoming query params and their values. A beautiful python package voluptuous is being used for validations on the incoming query parameters. The best part about voluptuous is you can define your own validations as per your query params requirements.

Quick start


Installation

  1. Download drf-url-filters app package from this git repo or can be installed using python-pip like pip install drf-url-filters.

  2. Add filters in INSTALLED_APPS of settings.py file of django project.

How it works

  1. Your View or ModelViewSet should inherit FiltersMixin from filters.mixins.FiltersMixin.

  2. To apply filters using drf-url-filters we need to configure our view to have a dict mapping filter_mappings which converts incoming query parameters to query you want to make on the column name on the queryset.

  3. Optionally, to perform any preprocessing on the incoming values for query params, add another dict filter_value_transformations which maps incoming query parameters to functions that should be applied to the values corresponding to them. The resultant value is used in the final filtering.

validations.py

import six

from filters.schema import base_query_params_schema
from filters.validations import (
    CSVofIntegers,
    IntegerLike,
    DatetimeWithTZ
)

# make a validation schema for players filter query params
players_query_schema = base_query_param_schema.extend(
    {
        "id": IntegerLike(),
        "name": six.text_type,  # Depends on python version
        "team_id": CSVofIntegers(),  # /?team_id=1,2,3
        "install_ts": DatetimeWithTZ(),
        "update_ts": DatetimeWithTZ(),
        "taller_than": IntegerLike(),
    }
)

views.py

from rest_framework import (
    viewsets,
    filters,
)

from .models import Player, Team
from .pagination import ResultSetPagination
from .serializers import PlayerSerializer, TeamSerializer
from .validations import teams_query_schema, players_query_schema
from filters.mixins import (
    FiltersMixin,
)


class PlayersViewSet(FiltersMixin, viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    """
    queryset = Player.objects.prefetch_related(
        'teams'  # use prefetch_related to minimize db hits.
    ).all()
    serializer_class = PlayerSerializer
    pagination_class = ResultSetPagination
    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ('id', 'name', 'update_ts')
    ordering = ('id',)

    # add a mapping of query_params to db_columns(queries)
    filter_mappings = {
        'id': 'id',
        'name': 'name__icontains',
        'team_id': 'teams',
        'install_ts': 'install_ts',
        'update_ts': 'update_ts',
        'update_ts__gte': 'update_ts__gte',
        'update_ts__lte': 'update_ts__lte',
        'taller_than': 'height__gte',
    }

    filter_value_transformations = {
        'taller_than': lambda val: val / 30.48  # cm to ft
    }

    # add validation on filters
    filter_validation_schema = players_query_schema

With the use of drf-url-filters adding a new filter on a new column is as simple as adding a new key in the dict. Prohibitting a filter on particular column is same as removing a key value mapping from the filter_mappings dict.

LICENSE

MIT License Copyright (c) 2016 Manjit Kumar.

Credits

Special thanks to authors of voluptuous and friends cdax and saurabhjha who encourage people to contribute into open source community.

Support

Please open an issue for support.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].