Password Not Encrypted During Save Update

I am trying to create a user using api. With my post/put request, my password is not saved in encrypted form. My code is as below.

from urllib import request
from rest_framework import viewsets, status
from django.contrib.auth.models import User
from atest.serializers import UserSerializer
from rest_framework import permissions
from atest.permissions import IsOwnerOrReadOnly
from rest_framework.decorators import action
from rest_framework.response import Response

class UserViewSet(viewsets.ModelViewSet):
    This viewset provides operations on Users table to the same user.

    # permission_classes = [IsOwnerOrReadOnly,]
    queryset = User.objects.all()
    serializer_class = UserSerializer

from rest_framework import serializers
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password

class UserSerializer(serializers.HyperlinkedModelSerializer):
    password = serializers.CharField(max_length=128, style={'input_type': 'password'}, write_only=True)

    class Meta:
        model = User
        fields = ('url', 'id', 'username', 'email', 'first_name', 'last_name', 'password')

        def create(self, validated_data):
            username = validated_data['username']
            email = validated_data['email']
            first_name = validated_data['first_name']
            last_name = validated_data['last_name']
            password = make_password(validated_data['password'])

        def update(self, instance, validated_data):
   = validated_data.get('email',
            instance.username = validated_data.get('username', instance.username)
            instance.first_name = validated_data.get('first_name', instance.first_name)
            instance.last_name = validated_data.get('last_name', instance.last_name)
            instance.password = make_password(validated_data.get('password', instance.password))
            return instance

Please help me identifying the solution. What am I doing wrong that my password is not getting encrypted?



You're just assigning things to a local variable in create; in addition, you shouldn't be calling make_password in update with instance.password; that way you'll be re-hashing the hashed password in the instance, making it unusable.

There happens to be an example (slightly roundabout) for this in the DRF documentation that might help you along:

class CreateUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'username', 'password')
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = User(
        return user