Monday, May 3, 2021
#Django100days
This code helps you to update the foreign key automatically after saving the serializer in views.
class CreateGroup(APIView):
"""
Provides a post method handler.
"""
def post(self, request , *args, **kwargs):
serializer = GroupSerializer(data=request.data)
if serializer.is_valid():
newGroup = serializer.save() #Saving the serializer
update_s = CustomUser.objects.get(email = request.user) # getting th current logged in user
update_s.group = newGroup // assigning newly created group to user
print(newGroup)
update_s.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors,status=400)
0 Response to Automatically update foreign key in django rest framework using serializer
Comments are personally moderated by our team. Promotions are not encouraged.
Post a Comment