in djnago rest framework what is difference between self.request and request in why we cant always use request and exactly in what situations we need to use self.request or request
class MyView(APIView):
def post(self, request, format=None):
data = self.request.data
login(request, user)
i try to print them and both of them return same thing
<rest_framework.request.Request: POST '/url/sub_url'>
so why we user like
data = self.request.data
login(request, user)
the request argument is passed to the post method. like any normal function that you can define and use its arguments.
But since post is a method it takes self argument. you can access the class methods and attributes including request.
And they're the same.
When request is passed to your function just use request but if not and you need request use self.request.
If you are using function based views, you wont be able to use self.request. Here as you are using class based views, you can access it both ways.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With