-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from iashraful/feature/issue#5-filtering
Issue #5 Enable filtering options
- Loading branch information
Showing
5 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from copy import deepcopy | ||
|
||
from django.db.models import Q | ||
|
||
|
||
class APIFilteredMixin: | ||
""" | ||
DO NOT INCLUDE THIS CLASS TO ANY MODEL, VIEW OR ANYWHERE | ||
Basically it'll parse the request params and return django queryset format | ||
""" | ||
|
||
|
||
@classmethod | ||
def get_filters(cls, model, request, **kwargs): | ||
""" | ||
model: | ||
""" | ||
_fields = [f.name for f in model._meta.get_fields()] | ||
_params = request.GET | ||
_filters = {} | ||
for param, val in _params.items(): | ||
try: | ||
if ':' in param: | ||
_field_name, _filter_option = param.split(':') | ||
if _field_name not in _fields: | ||
continue | ||
_temp = deepcopy(param) | ||
_filters['{}__{}'.format(_field_name, _filter_option)] = val | ||
else: | ||
if param not in _fields: | ||
continue | ||
_filters[param] = val | ||
except Exception as error: | ||
continue | ||
return Q(**_filters) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters