I'm trying to pass the requested "userid" to the Stored Procedure but I couldn't able to get the requested "userid" in GET method. SP is working fine when I entered user id directly
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':
user = Tblclaimreference.objects.get(userid=self.request.user.id)
print(user)
cursor = connection.cursor()
cursor.execute('EXEC [dbo].[sp_GetClaims] @UserId= %s',(user,))
result_set = cursor.fetchall()
print(type(result_set))
response_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)
elif request.method == 'POST':
serializer = ClaimReferenceSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
models.py:
class Tblclaimreference(models.Model):
taskid = models.IntegerField(db_column='TaskId', primary_key=True)
userid = models.IntegerField(db_column='UserID')
internaldescription = models.CharField(db_column='Internaldescription', max_length=500, blank=True, null=True)
additionalcomments = models.CharField(db_column='Additionalcomments', max_length=500, blank=True, null=True)
CodePudding user response:
You pass a Tblclaimreference
, not a user id. You can use:
cursor.execute('EXEC [dbo].[sp_GetClaims] @UserId= %s',(userid,))
If you want to use the .pk
of the logged in user, it is:
cursor.execute('EXEC [dbo].[sp_GetClaims] @UserId= %s',(request.user.pk,))