[Fixed]-Django Rest Framework โ€“ Extending the existing User model and saving it

1๐Ÿ‘

โœ…

If you want to save the currently loggedin user, you can override the perform_create() method of the ProfileCreateAPIView.

In that method, you can pass the user argument with value as request.user. DRF will use this extra value along with the fields defined in the serializer to create Profile instance.

class ProfileCreateAPIView(CreateAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileCreateSerializer

    def perform_create(self, serializer):        
        serializer.save(user=self.request.user) # pass the user field 

Leave a comment