Home > Enterprise >  Cannot Iterate through the array list in Django
Cannot Iterate through the array list in Django

Time:12-16

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:

  @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)

CodePudding user response:

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)
  • Related