I've never found a django-filter that worked as I needed it to. I prefer this simple solution that you should be able to use to solve your problem:
views.py
# inherits APIView, ModelViewSet, etc.
class View(...):
# override get_queryset method like so:
def get_queryset(self):
queryset = self.queryset
custom_filter = self.request.query_params.get('filter')
if custom_filter:
custom_filter = json.loads(custom_filter)
queryset = queryset.filter(**custom_filter)
return queryset
Then we can filter from javascript like so:
script.js
// create the django-like filter:
var filter = JSON.stringify({
'modal__name' : 'My Modal Name',
})
var url = '.../api/test?filter=' + filter;
Your url will look like: .../api/test?filter={"modal__name":"My Modal Name"}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…