You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+47-24Lines changed: 47 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ on incoming query params and their values. A beautiful python package voluptouos
7
7
# Quick start
8
8
---
9
9
**Installation**
10
-
10
+
11
11
1. Download `drf-url-filters` app package from this git repo or can be isnatlled using python-pip like `pip install drf-url-filters`.
12
12
13
13
2. Add `filters` in INSTALLED_APPS in settings.py file of django project.
@@ -16,73 +16,96 @@ on incoming query params and their values. A beautiful python package voluptouos
16
16
17
17
1. Your View or ModelViewSet should inherit `FiltersMixin` from filters.mixins.FiltersMixin .
18
18
19
-
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.
19
+
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.
20
20
21
-
```python
22
21
# validations.py
22
+
23
+
```python
24
+
25
+
from filters.schema import base_query_param_schema
23
26
from filters.validations import (
24
27
CSVofIntegers,
25
28
IntegerLike,
26
29
DatetimeWithTZ
27
30
)
28
31
29
-
from filters.schema import base_query_param_schema
30
-
32
+
# make a validation schema for players filter query params
This viewset automatically provides `list`, `create`,
52
-
`retrieve`, `update` and `destroy` actions.
64
+
This viewset automatically provides `list`, `create`, `retrieve`,
65
+
`update` and `destroy` actions.
53
66
"""
54
-
serializer_class = PlayersSerializer
55
-
pagination_class = PlayersPagination
56
-
57
-
# add a mapping of query_params to db_columns(queries)
67
+
serializer_class = PlayerSerializer
68
+
pagination_class = ResultSetPagination
69
+
filter_backends = (filters.OrderingFilter,)
70
+
ordering_fields = ('id', 'name', 'update_ts')
71
+
ordering = ('id',)
72
+
73
+
# add a mapping of query_params to db_columns(queries)
58
74
filter_mappings = {
59
75
'id': 'id',
60
76
'name': 'name__icontains',
61
-
'team_id': 'teams',# considering a many-to-many related field
77
+
'team_id': 'teams',
62
78
'install_ts': 'install_ts',
63
79
'update_ts': 'update_ts',
64
80
'update_ts__gte': 'update_ts__gte',
65
81
'update_ts__lte': 'update_ts__lte',
66
82
}
83
+
67
84
# add validation on filters
68
85
filter_validation_schema = players_query_schema
69
-
86
+
70
87
defget_queryset(self):
71
88
"""
72
89
Optionally restricts the queryset by filtering against
73
90
query parameters in the URL.
74
91
"""
75
92
query_params =self.request.query_params
76
-
queryset = Players.objects.all()
93
+
queryset = Player.objects.prefetch_related(
94
+
'teams'# use prefetch_related to minimize db hits.
95
+
).all()
96
+
77
97
# This dict will hold filter kwargs to pass in to Django ORM calls.
78
98
db_filters = {}
79
-
# update filters on players queryset using FiltersMixin.get_queryset_filters
80
-
db_filters.update(
99
+
100
+
# update filters dict with incoming query params and then pass as
101
+
# **kwargs to queryset.filter()
102
+
db_filters.update(
81
103
self.get_queryset_filters(
82
104
query_params
83
105
)
84
106
)
85
107
return queryset.filter(**db_filters)
108
+
86
109
```
87
110
88
111
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 a key value mapping from the `filter_mappings` dict.
Copy file name to clipboardExpand all lines: docs/README.md
+46-23Lines changed: 46 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ on incoming query params and their values. A beautiful python package voluptouos
7
7
# Quick start
8
8
---
9
9
**Installation**
10
-
10
+
11
11
1. Download `drf-url-filters` app package from this git repo or can be isnatlled using python-pip like `pip install drf-url-filters`.
12
12
13
13
2. Add `filters` in INSTALLED_APPS in settings.py file of django project.
@@ -16,73 +16,96 @@ on incoming query params and their values. A beautiful python package voluptouos
16
16
17
17
1. Your View or ModelViewSet should inherit `FiltersMixin` from filters.mixins.FiltersMixin .
18
18
19
-
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.
19
+
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.
20
20
21
-
```python
22
21
# validations.py
22
+
23
+
```python
24
+
25
+
from filters.schema import base_query_param_schema
23
26
from filters.validations import (
24
27
CSVofIntegers,
25
28
IntegerLike,
26
29
DatetimeWithTZ
27
30
)
28
31
29
-
from filters.schema import base_query_param_schema
30
-
32
+
# make a validation schema for players filter query params
This viewset automatically provides `list`, `create`,
52
-
`retrieve`, `update` and `destroy` actions.
64
+
This viewset automatically provides `list`, `create`, `retrieve`,
65
+
`update` and `destroy` actions.
53
66
"""
54
-
serializer_class = PlayersSerializer
55
-
pagination_class = PlayersPagination
56
-
57
-
# add a mapping of query_params to db_columns(queries)
67
+
serializer_class = PlayerSerializer
68
+
pagination_class = ResultSetPagination
69
+
filter_backends = (filters.OrderingFilter,)
70
+
ordering_fields = ('id', 'name', 'update_ts')
71
+
ordering = ('id',)
72
+
73
+
# add a mapping of query_params to db_columns(queries)
58
74
filter_mappings = {
59
75
'id': 'id',
60
76
'name': 'name__icontains',
61
-
'team_id': 'teams',# considering a many-to-many related field
77
+
'team_id': 'teams',
62
78
'install_ts': 'install_ts',
63
79
'update_ts': 'update_ts',
64
80
'update_ts__gte': 'update_ts__gte',
65
81
'update_ts__lte': 'update_ts__lte',
66
82
}
83
+
67
84
# add validation on filters
68
85
filter_validation_schema = players_query_schema
69
-
86
+
70
87
defget_queryset(self):
71
88
"""
72
89
Optionally restricts the queryset by filtering against
73
90
query parameters in the URL.
74
91
"""
75
92
query_params =self.request.query_params
76
-
queryset = Players.objects.all()
93
+
queryset = Player.objects.prefetch_related(
94
+
'teams'# use prefetch_related to minimize db hits.
95
+
).all()
96
+
77
97
# This dict will hold filter kwargs to pass in to Django ORM calls.
78
98
db_filters = {}
79
-
# update filters on players queryset using FiltersMixin.get_queryset_filters
99
+
100
+
# update filters dict with incoming query params and then pass as
101
+
# **kwargs to queryset.filter()
80
102
db_filters.update(
81
103
self.get_queryset_filters(
82
104
query_params
83
105
)
84
106
)
85
107
return queryset.filter(**db_filters)
108
+
86
109
```
87
110
88
111
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 a key value mapping from the `filter_mappings` dict.
0 commit comments