I have to display a calculated value in APIVIEW, but I can’t figure out how to set up the view, it’s giving me an error.
The code, that returns a simple JSON is working fine:
def protein_coverage(request, protein_id):
try:
proteins = Protein.objects.filter(protein=protein_id)
domain_length = 0
coverage = domain_length / protein_length
except Protein.DoesNotExist:
return HttpResponse({'message': 'This Protein does not exist'}, status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = ProteinCoverageSerializer(coverage)
return JsonResponse(serializer.data,safe=False)
I tried this for the APIView:
class ProteinCoverage(generics.RetrieveAPIView):
serializer_class = ProteinCoverageSerializer
def get_queryset(self):
pk = self.kwargs['protein_id']
proteins = Protein.objects.filter(protein=pk)
domain_length = 0
coverage = domain_length / protein_length
return coverage
But it’s giving me an error:
Expected view ProteinCoverage to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
I’m not sure, which API is suitable for this situation and how to pass a single variable to it.
I also checked the documentation, but it’s not clear.
How do I convert this JsonResponse to APIView?
2
Answers
You have to use
lookup_fields
. Refer docsYou can simply do it with APIView: