Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot Iterate through the array list in Django

I’m iterating through all the records from database but only one the first row is reflecting in the output. The database data which are list of tuples

 [(('HR749', datetime.datetime(2021, 11, 5, 20, 0, 17), 'Web', 'Referrals ', 'Draft', 'Bus', 'India', 'satish', 10902, 'Openings', datetime.date(2021, 11, 10),('HR855', datetime.datetime(2021, 11, 5, 20, 11, 41), 'Web', 'Referrals ', 'Draft', 'BusS', 'India', 'mah', 83837, ' referral', datetime.date(2021, 11, 10)), ('HR929', datetime.datetime(2021, 11, 5, 20, 22, 58), 'Web', 'Referrals ', 'Draft', 'BusS', 'India', 'ritika', 6124, 'Unable to submit', datetime.date(2021, 11, 10))]

Here, what I have tried I know its simple but I don’t know why I couldn’t able to get all the records from the database

views.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  @api_view(['GET', 'POST'])
def ClaimReferenceView(request,userid):
    try:
        userid = Tblclaimreference.objects.filter(userid=userid)
      
    except Tblclaimreference.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':

        userID = request.data.get(userid)
        print(userID)
        cursor = connection.cursor()
        cursor.execute('EXEC [dbo].[sp_GetClaims]  @UserId= %s',('10',))

        result_set = cursor.fetchall()
        print(type(result_set))
        print(result_set)
        
        for row in result_set:
            
          Number= row[0]
          Opened = row[1]
          Contacttype = row[2]
          Category1 = row[3]
          State = row[4]
          Assignmentgroup = row[5]
          Country_Location = row[6]
          Openedfor = row[7]
          Employeenumber = row[8]
          Shortdescription = row[9]
          AllocatedDate = row[10]
       

        return Response({"Number":Number,"Opened":Opened, "Contacttype": Contacttype, "Category1":Category1, 
        "State":State, "Assignmentgroup":Assignmentgroup, "Country_Location": Country_Location, "Openedfor":Openedfor,
         "Employeenumber":Employeenumber, "Shortdescription": Shortdescription, "AllocatedDate":AllocatedDate}, status=status.HTTP_200_OK)

>Solution :

I would suggest you use django’s ORM instead of writing raw SQL.
However, in your case, what you need to do is make a list with all the rows (response_data) and append each dictionary to it.
This should work:

def ClaimReferenceView(request,userid):
    try:
        userid = Tblclaimreference.objects.filter(userid=userid)
      
    except Tblclaimreference.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':

        userID = request.data.get(userid)
        print(userID)
        cursor = connection.cursor()
        cursor.execute('EXEC [dbo].[sp_GetClaims]  @UserId= %s',('10',))

        result_set = cursor.fetchall()
        print(type(result_set))
        print(result_set)
        
        respone_data = []
        for row in result_set:
            response_data.append(
                {
                    "Number": row[0],
                    "Opened": row[1], 
                    "Contacttype": row[2], 
                    "Category1": row[3],
                    "State": row[4], 
                    "Assignmentgroup": row[5], 
                    "Country_Location": row[6], 
                    "Openedfor": row[7],
                    "Employeenumber": row[8], 
                    "Shortdescription": row[9], 
                    "AllocatedDate": row[10]
                } 
            )

        return Response(response_data, status=status.HTTP_200_OK)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading