Creating Rest Api in python using JSON?
<p>I'm new to python REST API and so facing some particular problems. I want that when I enter the input as pathlabid(primary key), I want the corresponding data assigned with that key as output. When I run the following code i only get the data corresponding to the first row of table in database even when the id i enter belong to some other row.
This is the VIEWS.PY </p>

<pre><code>class pathlabAPI(View):
@csrf_exempt
def dispatch(self, *args, **kwargs):
    # dont worry about the CSRF here
    return super(pathlabAPI, self).dispatch(*args, **kwargs)

def post(self, request):
    post_data = json.loads(request.body)
    Pathlabid = post_data.get('Pathlabid') or ''
    lablist = []
    labdict = {}

    lab = pathlab()
    labs = lab.apply_filter(Pathlabid = Pathlabid)


    if Pathlabid :


        for p in labs:
                labdict["Pathlabid"] = p.Pathlabid
                labdict["name"] = p.Name
                labdict["email_id"] = p.Emailid
                labdict["contact_no"] = p.BasicContact
                labdict["alternate_contact_no"] = p.AlternateContact
                labdict["bank_account_number"] = p.Accountnumber
                labdict["ifsccode"] = p.IFSCcode
                labdict["country"] = p.Country
                labdict["homepickup"] = p.Homepickup
                lablist.append(labdict)
                return HttpResponse(json.dumps(lablist))
    else:

        for p in labs:
                labdict["bank_account_number"] = p.Accountnumber
                lablist.append(labdict)
                return HttpResponse(json.dumps(lablist))
</code></pre>

<p>There are a number of issues with the overall approach and code but to fix the issue you're describing, but as a first fix I agree with the other answer: you need to take the <code>return</code> statement out of the loop. Right now you're <code>return</code>ing your list as soon as you step through the loop one time, which is why you always get a list with one element. Here's a fix for that (you will need to add <code>from django.http import JsonResponse</code> at the top of your code):</p>

<pre><code>if Pathlabid:
    for p in labs:
        labdict["Pathlabid"] = p.Pathlabid
        labdict["name"] = p.Name
        labdict["email_id"] = p.Emailid
        labdict["contact_no"] = p.BasicContact
        labdict["alternate_contact_no"] = p.AlternateContact
        labdict["bank_account_number"] = p.Accountnumber
        labdict["ifsccode"] = p.IFSCcode
        labdict["country"] = p.Country
        labdict["homepickup"] = p.Homepickup
        lablist.append(labdict)
else:
    for p in labs:
        labdict["bank_account_number"] = p.Accountnumber
        lablist.append(labdict)

return JsonResponse(json.dumps(lablist))
</code></pre>

<p>As suggested in the comments, using Django Rest Framework or a similar package would be an improvement. As a general rule, in Django or other ORMs, you want to avoid looping over a queryset like this and adjusting each element. Why not serialize the queryset itself and do the logic that's in this loop in your template or other consumer?</p>

python json django rest api