'Expected view Equation1View to be called with a URL keyword argument named "pk"

I'm trying to get data directly from request body in json. Making calculations in API will be main part of my Django REST Api. In the future I'm going to add database from which I'll be getting some data that will be used in calculations.

I'm having a problem with error AssertionError: Expected view Equation1View 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 don't know where should I put the pk parameter. Using the api below I should be able to send {"name":"xxx", value:1.23} in request body and get {"emission":1.23} in json response. Ofcourse it will be more complicated in future as getting more data from request body and connecting it with data from db but now I have to make it simple.

api urls.py:

from .views import Equation1View
from django.urls import path

urlpatterns = [
   path('equation/1', Equation1View.as_view())
   #path('equation/2'),
   #path('equation/3'), 
]

views.py

from aiohttp import request
from rest_framework import generics
from .serializers import EmissionSerializer, EquaSerializer

# Create your views here.
class Equation1View(generics.RetrieveAPIView):
    queryset = ''
    serializer_class = EquaSerializer(context={'request':request})

serializers.py

from rest_framework import serializers

class EquaSerializer(serializers.Serializer):
    emission = serializers.SerializerMethodField('perform_equation')

    def perform_equation(self):
        request = self.context.get("request")
        if request and hasattr(request, 'name') and hasattr(request, 'value'):
            return request.value


Solution 1:[1]

RetrieveAPIView is generic api view to retrieve specific data from the model and that data is retrieved based on the pk that is passed through the url so change your urls.py as

from .views import Equation1View
from django.urls import path

urlpatterns = [
   path('equation/<int:pk>', Equation1View.as_view())
   #path('equation/2'),
   #path('equation/3'), 
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user8193706