Skip to content

Commit

Permalink
Add the request to the context for the serializer
Browse files Browse the repository at this point in the history
This is useful if subclassing the views and using custom serializers.

Use case: I set up a custom SignupSerializer that needs to validate the email address to make sure the domain matches the current user - the current user needs to be found from request.user
  • Loading branch information
pjrobertson committed Jul 24, 2022
1 parent 85abc4b commit fe9cbca
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions authemail/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Signup(APIView):
serializer_class = SignupSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
email = serializer.data['email']
Expand Down Expand Up @@ -100,7 +100,7 @@ class Login(APIView):
serializer_class = LoginSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
email = serializer.data['email']
Expand Down Expand Up @@ -150,7 +150,7 @@ class PasswordReset(APIView):
serializer_class = PasswordResetSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
email = serializer.data['email']
Expand Down Expand Up @@ -207,7 +207,7 @@ class PasswordResetVerified(APIView):
serializer_class = PasswordResetVerifiedSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
code = serializer.data['code']
Expand Down Expand Up @@ -237,7 +237,7 @@ class EmailChange(APIView):
serializer_class = EmailChangeSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
user = request.user
Expand Down Expand Up @@ -322,7 +322,7 @@ class PasswordChange(APIView):
serializer_class = PasswordChangeSerializer

def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
serializer = self.serializer_class(data=request.data, context={'request': request})

if serializer.is_valid():
user = request.user
Expand All @@ -344,4 +344,4 @@ class UserMe(APIView):
serializer_class = UserSerializer

def get(self, request, format=None):
return Response(self.serializer_class(request.user).data)
return Response(self.serializer_class(request.user, context={'request': request}).data)

0 comments on commit fe9cbca

Please sign in to comment.