Reputation: 317
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)
Upvotes: 3
Views: 1063
Reputation: 4229
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.
Upvotes: 1
Reputation: 2380
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
.
Upvotes: 6