0

Im trying to pass several list to my URL through AJAX for filter purposes in my REST APIVIEW, but I couldn't get it right. i try to print using request.get function as well, but its now printing anything. below is my code :

Javascript :

//assume the rest of the array contain id like the list below
search_type_list = ['2', '4', '244, '42',]

a = "/api/dashboard/project_workorder_filter/{{selected_project.id}}/?search_type_list=" + search_type_value.join(',') + "/?parent_list=" + parent_value.join(',') + "/?status_list=" + status_value.join(',') + "/?task_list=" + task_value.join(',') + "/?user_list=" + user_value.join(',') + "/";
        console.log(a)

        content_workorder_filter_json = $.ajax({
            type: "GET",
            url: a,
            dataType: "application/json",
            async: false
        }).responseText;
        content_workorder_filter = JSON.parse(content_workorder_filter_json);
        console.log(content_workorder_filter)

URl :

  url(r'^api/dashboard/project_workorder_filter/(?P<project_id>\d+)/(?P<search_type_list>\w{0,1000})/(?P<parent_list>\w{0,1000})/(?P<status_list>\w{0,1000})/(?P<task_list>\w{0,1000})/(?P<user_list>\w{0,1000})/$', api.ProjectWorkorderFilterAPI.as_view(), name='project_workorder_filter'),

API.py

class ProjectWorkorderFilterAPI(APIView):
    def get(self, request, project_id, search_type_list,format=None):
        a = request.GET.get('search_type_list')
        print(a)
        model_object = WorkOrder.objects.filter(parent_project_content__project=project_id, search_type__id__in=[search_type_list], parent_project_content__parent__in=[parent_list], status__id__in=[status_list], task__id__in=[task_list], assign_to__id__in=[user_list] )
        serializer = ProjectWorkorderSerializer(model_object, many=True)
        return Response(serializer.data)
M.Izzat
  • 1,086
  • 4
  • 22
  • 50

1 Answers1

0

You made few mistakes here:

  • request.GET contains query string params not url params
  • you added ? in /?search_type_list (so it wasn't passed)
  • you shouldn't use url params for that.

You should use a POST request or add params in query string. This is how list params should be past in query string.

Uri:

url(r'^api/dashboard/project_workorder_filter/(?P<project_id>\d+)api.ProjectWorkorderFilterAPI.as_view(), name='project_workorder_filter')

for your example you should remove all the lists in django's url(). Actually request.GET contains the variables from query string, not the url params.

Summing up

You should generate a query params list like in the code below and add it to original url. Then in django view pick those params from request.GET:

Javascript:

let queryString = '?search_type_list='
queryString += search_type_value.join('&search_type_list=');
let parentListQueryString = '&';
parentListQueryString += parent_value.join('&parent_value=');
queryString += parentListQueryString;
Community
  • 1
  • 1
mdargacz
  • 1,267
  • 18
  • 32