简体   繁体   中英

How to access the Data of an model using particular id in django rest framework

I want to access the data of an model which is named as IssueReport by using it's ID in django rest framework so I used the post method for passing the ID manually to get the IssueReport data by that ID but it gives me the Error as,

Method Not Allowed: /app/auth/getissuereport/
[10/Aug/2022 23:26:21] "GET /app/auth/getissuereport/ HTTP/1.1" 405 7036
Internal Server Error: /app/auth/getissuereport/
Traceback (most recent call last):
  File "D:\MMRDA\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "D:\MMRDA\.venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\MMRDA\.venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\MMRDA\.venv\lib\site-packages\django\views\generic\base.py", line 84, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "D:\MMRDA\.venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\MMRDA\mmrda\apis\views.py", line 195, in post
    report=IssueReport.objects.get(pk=id)
  File "D:\MMRDA\.venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\MMRDA\.venv\lib\site-packages\django\db\models\query.py", line 496, in get
    raise self.model.DoesNotExist(
DataSet.models.IssueReport.DoesNotExist: IssueReport matching query does not exist.

This is my views.py,

class GetIssueReportById(generics.GenericAPIView):

    serializer_class=GetIssueReportSerializer
    def post(self,request,*args,**kwargs):
        serializer=self.get_serializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            id=serializer.data.get('id')
            report=IssueReport.objects.get(pk=id)
            data=IssueReportSerializer(report,context=self.get_serializer_context()).data
            return Response({
                'message':"Data Fetched successfully",
                'status':"success",
                'data':data,
                },status=status.HTTP_200_OK)
        else:
            return Response({
                'message':"Data not fetched",
                'status':status.HTTP_400_BAD_REQUEST
                })

this is my serializers.py,

class IssueReportSerializer(serializers.ModelSerializer):
    class Meta:
        model=IssueReport
        fields='__all__'

class GetIssueReportSerializer(serializers.ModelSerializer):
    class Meta:
        model=IssueReport
        fields=('id',)

So can someone help me out to access the data by using ID only or any other way, Thank you in advance.

You obviously sent GET request to your server, but in class GetIssueReportById(generics.GenericAPIView): you declared only post method.

Either define def get(): method in class GetIssueReportById , or send POST request to the server (may be with some REST client from browser plugin or VS Code plugin). If you get id argument with id=serializer.data.get('id') , id field must be in POST body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM