Package: drf-url-filters
drf-url-filtersdrf-url-filtersis 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 packagevoluptouosis being used
for validations on the incoming query parameters. The best part about
voluptouos is you can define your own validations as per your query
params requirements.# Quick startInstallationDownloaddrf-url-filtersapp package from this git repo or can be
installed using python-pip likepip installdrf-url-filters.Addfiltersin INSTALLED_APPS of settings.py file of django
project.How it worksYour View or ModelViewSet should inheritFiltersMixinfromfilters.mixins.FiltersMixin.To apply filters usingdrf-url-filterswe need to configure our
view to have a dict mappingfilter_mappingswhich converts
incoming query parameters to query you want to make on the column
name on the queryset.Optionally, to perform any preprocessing on the incoming values for
query params, add another dictfilter_value_transformationswhich
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.pyimportsixfromfilters.schemaimportbase_query_params_schemafromfilters.validationsimport(CSVofIntegers,IntegerLike,DatetimeWithTZ)# make a validation schema for players filter query paramsplayers_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.pyfromrest_frameworkimport(viewsets,filters,)from.modelsimportPlayer,Teamfrom.paginationimportResultSetPaginationfrom.serializersimportPlayerSerializer,TeamSerializerfrom.validationsimportteams_query_schema,players_query_schemafromfilters.mixinsimport(FiltersMixin,)classPlayersViewSet(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=PlayerSerializerpagination_class=ResultSetPaginationfilter_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',}field_value_transformations={'taller_than':lambdaval:val/30.48# cm to ft}# add validation on filtersfilter_validation_schema=players_query_schemaWith the use ofdrf-url-filtersadding 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 thefilter_mappingsdict.LICENSEMIT LicenseCopyright (c) 2016 Manjit Kumar.CreditsSpecial thanks to authors ofvoluptouosand friendscdaxandsaurabhjhawho encourage people to
contribute into open source community.SupportPlease [open an issue]
(https://github.com/manjitkumar/drf-url-filters/issues/new) for support.