You do not need to get rid of the []
. The are good practice to indicate "I really do want to pass an array, i.e. multiple values for this query parameter".
The []
are presumably there because ember-data in your front end uses coalesceFindRequests
. Dustin Farras (the main author of the ember-Django-adapter) has written an article about using his adapter with coalesceFindRequests
: You can define a filter that covers []
and simply tell the django-rest-framework to use it:
# myapp/filters.py
from rest_framework import filters
class CoalesceFilterBackend(filters.BaseFilterBackend):
"""
Support Ember Data coalesceFindRequests.
"""
def filter_queryset(self, request, queryset, view):
id_list = request.query_params.getlist('ids[]')
if id_list:
# Disable pagination, so all records can load.
view.pagination_class = None
queryset = queryset.filter(id__in=id_list) # adapt here, see note below
return queryset
Now you just need to add this filter to filter_backends in your views, e.g.:
from myapp.filters import CoalesceFilterBackend
class UserListView(generics.ListAPIView):
queryset = User.objects.all()
serializer = UserSerializer
filter_backends = (CoalesceFilterBackend,)
Or, configure it globally in your DRF settings:
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('myapp.filters.CoalesceFilterBackend',) }
Note: You will need to adapt the filter for cities
, of course. (When filtering for primary keys, I prefer to filter
for pk__in=
instead of id__in=
that also covers primary keys with names other than id
.)