{"id":13765747,"url":"https://github.com/manjitkumar/drf-url-filters","last_synced_at":"2025-05-10T21:31:43.869Z","repository":{"id":6984734,"uuid":"55857140","full_name":"manjitkumar/drf-url-filters","owner":"manjitkumar","description":"A django app to apply filters on drf querysets using query params with validations using voluptuous.","archived":false,"fork":false,"pushed_at":"2023-06-20T05:54:10.000Z","size":1509,"stargazers_count":171,"open_issues_count":5,"forks_count":20,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-08-03T16:08:55.228Z","etag":null,"topics":["django","drf-url-filters","filters","python","queryset","url-filters"],"latest_commit_sha":null,"homepage":"http://www.django-rest-framework.org/api-guide/filtering/#drf-url-filters","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/manjitkumar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-04-09T17:16:17.000Z","updated_at":"2024-07-23T16:06:33.000Z","dependencies_parsed_at":"2024-04-18T17:57:58.501Z","dependency_job_id":"41a8960f-f488-4543-a504-acdd94bd5359","html_url":"https://github.com/manjitkumar/drf-url-filters","commit_stats":{"total_commits":34,"total_committers":12,"mean_commits":"2.8333333333333335","dds":0.7352941176470589,"last_synced_commit":"9bab112ceffb3807d6c045b1408c6be3f7baa2ab"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manjitkumar%2Fdrf-url-filters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manjitkumar%2Fdrf-url-filters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manjitkumar%2Fdrf-url-filters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manjitkumar%2Fdrf-url-filters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/manjitkumar","download_url":"https://codeload.github.com/manjitkumar/drf-url-filters/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224989429,"owners_count":17403408,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","drf-url-filters","filters","python","queryset","url-filters"],"created_at":"2024-08-03T16:00:45.020Z","updated_at":"2024-11-17T01:31:22.154Z","avatar_url":"https://github.com/manjitkumar.png","language":"Python","funding_links":[],"categories":["Packages","Python"],"sub_categories":["Filtering"],"readme":"# drf-url-filters\n\n[![Help Contribute to Open Source](https://www.codetriage.com/manjitkumar/drf-url-filters/badges/users.svg)](https://www.codetriage.com/manjitkumar/drf-url-filters)\n\n**drf-url-filters** is a simple django app to apply filters on drf\nmodelviewset's queryset in a clean, simple and configurable way. It also\nsupports validations on incoming query params and their values. A beautiful\npython package [voluptuous](https://github.com/alecthomas/voluptuous) is being\nused for validations on the incoming query parameters. The best part about\nvoluptuous is you can define your own validations as per your query params\nrequirements.\n\n# Quick start\n---\n**Installation**\n\n1. Download `drf-url-filters` app package from this git repo or can be\ninstalled using python-pip like `pip install drf-url-filters`.\n\n2. Add `filters` in INSTALLED_APPS of settings.py file of django project.\n\n**How it works**\n\n1. Your View or ModelViewSet should inherit `FiltersMixin` from\n`filters.mixins.FiltersMixin`.\n\n2. To apply filters using `drf-url-filters` we need to configure our view to\nhave a dict mapping `filter_mappings` which converts incoming query parameters\nto query you want to make on the column name on the queryset.\n\n3. Optionally, to perform any preprocessing on the incoming values for\nquery params, add another dict `filter_value_transformations` which maps\nincoming query parameters to functions that should be applied to the values\ncorresponding to them. The resultant value is used in the final filtering.\n\n# validations.py\n\n```python\nimport six\n\nfrom filters.schema import base_query_params_schema\nfrom filters.validations import (\n    CSVofIntegers,\n    IntegerLike,\n    DatetimeWithTZ\n)\n\n# make a validation schema for players filter query params\nplayers_query_schema = base_query_param_schema.extend(\n    {\n        \"id\": IntegerLike(),\n        \"name\": six.text_type,  # Depends on python version\n        \"team_id\": CSVofIntegers(),  # /?team_id=1,2,3\n        \"install_ts\": DatetimeWithTZ(),\n        \"update_ts\": DatetimeWithTZ(),\n        \"taller_than\": IntegerLike(),\n    }\n)\n```\n\n# views.py\n\n```python\n\nfrom rest_framework import (\n    viewsets,\n    filters,\n)\n\nfrom .models import Player, Team\nfrom .pagination import ResultSetPagination\nfrom .serializers import PlayerSerializer, TeamSerializer\nfrom .validations import teams_query_schema, players_query_schema\nfrom filters.mixins import (\n    FiltersMixin,\n)\n\n\nclass PlayersViewSet(FiltersMixin, viewsets.ModelViewSet):\n    \"\"\"\n    This viewset automatically provides `list`, `create`, `retrieve`,\n    `update` and `destroy` actions.\n    \"\"\"\n    queryset = Player.objects.prefetch_related(\n        'teams'  # use prefetch_related to minimize db hits.\n    ).all()\n    serializer_class = PlayerSerializer\n    pagination_class = ResultSetPagination\n    filter_backends = (filters.OrderingFilter,)\n    ordering_fields = ('id', 'name', 'update_ts')\n    ordering = ('id',)\n\n    # add a mapping of query_params to db_columns(queries)\n    filter_mappings = {\n        'id': 'id',\n        'name': 'name__icontains',\n        'team_id': 'teams',\n        'install_ts': 'install_ts',\n        'update_ts': 'update_ts',\n        'update_ts__gte': 'update_ts__gte',\n        'update_ts__lte': 'update_ts__lte',\n        'taller_than': 'height__gte',\n    }\n\n    filter_value_transformations = {\n        'taller_than': lambda val: val / 30.48  # cm to ft\n    }\n\n    # add validation on filters\n    filter_validation_schema = players_query_schema\n```\n\nWith the use of `drf-url-filters` adding a new filter on a new column is as\nsimple as adding a new key in the dict. Prohibitting a filter on particular\ncolumn is same as removing a key value mapping from the `filter_mappings` dict.\n\n\n# LICENSE\n[MIT License](LICENSE.MD)\nCopyright (c) 2016 Manjit Kumar.\n\n# Credits\nSpecial thanks to authors of\n[voluptuous](https://github.com/alecthomas/voluptuous) and friends\n[cdax](https://github.com/cdax) and [saurabhjha](https://github.com/SaurabhJha)\nwho encourage people to contribute into open source community.\n\n# Support\nPlease [open an issue](https://github.com/manjitkumar/drf-url-filters/issues/new) for support.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanjitkumar%2Fdrf-url-filters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanjitkumar%2Fdrf-url-filters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanjitkumar%2Fdrf-url-filters/lists"}