navnd
navnd

Reputation: 829

How to pass multiple values to one parameter in django rest api url for response?

We have created one django restapi for GET response, for this url is like this 127.0.0.1:8000/candidate/api/smarttest?package_id=1&chapter_id=1&level_type=easy

in above url we are only passing one id for those,bur we want to pass multiple ids to one parameter i.e chapter_id=[1,2,3] like this we want to pass

For this how can we pass,here is my code

I have tried with this code

def get(self,request):
    try:
        # import pdb;pdb.set_trace()
        response_dict={"status":True,"data":{}}
        package_id = request.GET['package_id']
        chapter_id = request.GET['chapter_id']
        level_type= request.GET.get('complex_id', '0')

        complexity_dict = {
            'easy': [1,2,3,4,5],
            'medium': [6,7,8,9,10]
        }

        question_set = QuestionSet.objects.filter(
            pkg_id__pk=package_id, chapter__pk=chapter_id
        )

        ibs_object = IbsTable.objects.filter(
            ibs_question_setmap__set_id__in=question_set,                           
            complexity__in=complexity_dict.get(level_type,range(0,11))
        )

        v_pks = [qq.pk for qq in ibs_object]
        data = SetContentDetails().splitting(request,v_pks)

        response_dict['data'] = data

    except Exception as e:
        ieonline_except_logger.critical(
            'candidate_management \t index \t '
            + str(e) 
            + '\n'
            + str(traceback.format_exc())
        )
        response_dict = {"status": False, "msg": Exception}
    return Response(response_dict)

Upvotes: 0

Views: 1367

Answers (1)

Lucas Weyne
Lucas Weyne

Reputation: 1152

To obtain a list of the data with the requested key, use QueryDict.getlist(key, default=None) (see the docs).

You code will be something like:

chapter_id = request.GET.getlist('chapter_id') # [1,2,3]

Then, pass the chapter_id list in your url

http://127.0.0.1:8000/candidate/api/smarttest?chapter_id=1&chapter_id=2&chapter_id=3

Upvotes: 2

Related Questions