@@ -17,7 +17,7 @@ installed using python-pip like `pip install drf-url-filters`.
1717
18182 . Add ` filters ` in INSTALLED_APPS of settings.py file of django project.
1919
20- ** How it works? **
20+ ** How it works**
2121
22221 . Your View or ModelViewSet should inherit ` FiltersMixin ` from
2323` filters.mixins.FiltersMixin ` .
@@ -26,6 +26,11 @@ installed using python-pip like `pip install drf-url-filters`.
2626have a dict mapping ` filter_mappings ` which converts incoming query parameters
2727to query you want to make on the column name on the queryset.
2828
29+ 3 . Optionally, to perform any preprocessing on the incoming values for
30+ query params, add another dict ` filter_value_transformations ` which maps
31+ incoming query parameters to functions that should be applied to the values
32+ corresponding to them. The resultant value is used in the final filtering.
33+
2934# validations.py
3035
3136``` python
@@ -46,6 +51,7 @@ players_query_schema = base_query_param_schema.extend(
4651 " team_id" : CSVofIntegers(), # /?team_id=1,2,3
4752 " install_ts" : DatetimeWithTZ(),
4853 " update_ts" : DatetimeWithTZ(),
54+ " taller_than" : IntegerLike(),
4955 }
5056)
5157```
@@ -88,6 +94,11 @@ class PlayersViewSet(FiltersMixin, viewsets.ModelViewSet):
8894 ' update_ts' : ' update_ts' ,
8995 ' update_ts__gte' : ' update_ts__gte' ,
9096 ' update_ts__lte' : ' update_ts__lte' ,
97+ ' taller_than' : ' height__gte' ,
98+ }
99+
100+ field_value_transformations = {
101+ ' taller_than' : lambda val : val / 30.48 # cm to ft
91102 }
92103
93104 # add validation on filters
0 commit comments