Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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?**
**How it works**

1. Your View or ModelViewSet should inherit `FiltersMixin` from
`filters.mixins.FiltersMixin`.
Expand All @@ -26,6 +26,11 @@ installed using python-pip like `pip install drf-url-filters`.
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

```python
Expand All @@ -46,6 +51,7 @@ players_query_schema = base_query_param_schema.extend(
"team_id": CSVofIntegers(), # /?team_id=1,2,3
"install_ts": DatetimeWithTZ(),
"update_ts": DatetimeWithTZ(),
"taller_than": IntegerLike(),
}
)
```
Expand Down Expand Up @@ -88,6 +94,11 @@ class PlayersViewSet(FiltersMixin, viewsets.ModelViewSet):
'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': lambda val: val / 30.48 # cm to ft
}

# add validation on filters
Expand Down
138 changes: 0 additions & 138 deletions docs/README.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/README.md
18 changes: 10 additions & 8 deletions filters/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __get_queryset_filters(self, query_params, *args, **kwargs):

if getattr(self, 'filter_mappings', None) and query_params:
filter_mappings = self.filter_mappings
value_transformations = getattr(self, 'filter_value_transformations', {})

try:
# check and raise 400_BAD_REQUEST for invalid query params
Expand All @@ -46,15 +47,16 @@ def __get_queryset_filters(self, query_params, *args, **kwargs):
# [1] ~ sign is used to exclude a filter.
is_exclude = '~' in query
if query in self.filter_mappings and value:
query = filter_mappings[query]
query_filter = filter_mappings[query]
transform_value = value_transformations.get(query, lambda val: val)
transformed_value = transform_value(value)
# [2] multiple options is filter values will execute as `IN` query
if isinstance(value, list):
query += '__in'
if value:
if is_exclude:
excludes.append((query, value))
else:
filters.append((query, value))
if isinstance(value, list) and not query_filter.endswith('__in'):
query_filter += '__in'
if is_exclude:
excludes.append((query_filter, transformed_value))
else:
filters.append((query_filter, transformed_value))

return dict(filters), dict(excludes)

Expand Down