to filter the database record with GET params is easily, but trouble, so i wrote this plugin for DRF3, hope it works for you
to filter record you need to create a class base on BaseFilter, then write a dict fields
with format {'params': 'django_orm_conditions'}
- project/api/filter.py
from request_params_filter.filter import BaseFilter
class CustomFilter(BaseFilter):
fields = {
'date': 'datetime_startswith',
'name': 'name',
'max_amount': 'amount__gte',
'min_amount': 'amount__lte'
}
- project/api/viewsets/test_viewset.py
from test.models import Test
from project.api.filter import CustomFilter
class TestViewSet(viewset.Viewset):
def list(self, request):
queryset = Test.objects.filter(user=request.user).all()
filter_obj = CustomFilter(request.query_params, queryset)
# normal
queryset = filter_obj.conditions_queryset()
# to skip some params
# queryset = filter_obj.conditions_queryset(['max_amount', 'min_amount'])
return Response(queryset)
- django.db.models.Q()