from rest_framework import serializers
from .models import CFUser


class CFUserSerializer(serializers.ModelSerializer):
    """
    Serializes a CFUser Model
    """
    user__password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = CFUser
        fields = ('id', 'user__email', 'full_name', 'user__password', 'avatar', 'created_at', 'user__date_joined')

        def create(self, validated_data):
            return CFUser.objects.create(**validated_data)

        def update(self, instance, validated_data):
            pass
from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers
from .models import CFUser


class CFUserSerializer(serializers.ModelSerializer):
    """
    Serializes a CFUser Model
    """
    password = serializers.CharField(write_only=True, required=False)
    confirm_password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = CFUser
        fields = ('id', 'email', 'full_name', 'created_at', 'updated_at', 'password', 'confirm_password')

        read_only_fields = ('created_at', 'updated_at')

        def create(self, validated_data):
            return CFUser.objects.create(**validated_data)

        def update(self, instance, validated_data):
            instance.email = validated_data('email', instance.email)

            instance.save()

            password = validated_data.get('password', None)
            confirm_password = validated_data.get('confirm_password', None)

            if password and confirm_password and password == confirm_password:
                instance.set_password(password)
                instance.save()

            update_session_auth_hash(self.context.get('request'), instance)

            return instance
Complete update method of User Serializer
Complete update method of User Serializer

+ from django.contrib.auth import update_session_auth_hash
  from rest_framework import serializers
  from .models import CFUser
  
  
  class CFUserSerializer(serializers.ModelSerializer):
      """
      Serializes a CFUser Model
      """
-     user__password = serializers.CharField(write_only=True, required=False)
+     password = serializers.CharField(write_only=True, required=False)
+     confirm_password = serializers.CharField(write_only=True, required=False)
  
      class Meta:
          model = CFUser
-         fields = ('id', 'user__email', 'full_name', 'user__password', 'avatar', 'created_at', 'user__date_joined')
+         fields = ('id', 'email', 'full_name', 'created_at', 'updated_at', 'password', 'confirm_password')
+ 
+         read_only_fields = ('created_at', 'updated_at')
  
          def create(self, validated_data):
              return CFUser.objects.create(**validated_data)
  
          def update(self, instance, validated_data):
-             pass
+             instance.email = validated_data('email', instance.email)
+ 
+             instance.save()
+ 
+             password = validated_data.get('password', None)
+             confirm_password = validated_data.get('confirm_password', None)
+ 
+             if password and confirm_password and password == confirm_password:
+                 instance.set_password(password)
+                 instance.save()
+ 
+             update_session_auth_hash(self.context.get('request'), instance)
+ 
+             return instance
Complete update method of User Serializer
## Code Before:
from rest_framework import serializers
from .models import CFUser


class CFUserSerializer(serializers.ModelSerializer):
    """
    Serializes a CFUser Model
    """
    user__password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = CFUser
        fields = ('id', 'user__email', 'full_name', 'user__password', 'avatar', 'created_at', 'user__date_joined')

        def create(self, validated_data):
            return CFUser.objects.create(**validated_data)

        def update(self, instance, validated_data):
            pass
## Instruction:
Complete update method of User Serializer
## Code After:
from django.contrib.auth import update_session_auth_hash
from rest_framework import serializers
from .models import CFUser


class CFUserSerializer(serializers.ModelSerializer):
    """
    Serializes a CFUser Model
    """
    password = serializers.CharField(write_only=True, required=False)
    confirm_password = serializers.CharField(write_only=True, required=False)

    class Meta:
        model = CFUser
        fields = ('id', 'email', 'full_name', 'created_at', 'updated_at', 'password', 'confirm_password')

        read_only_fields = ('created_at', 'updated_at')

        def create(self, validated_data):
            return CFUser.objects.create(**validated_data)

        def update(self, instance, validated_data):
            instance.email = validated_data('email', instance.email)

            instance.save()

            password = validated_data.get('password', None)
            confirm_password = validated_data.get('confirm_password', None)

            if password and confirm_password and password == confirm_password:
                instance.set_password(password)
                instance.save()

            update_session_auth_hash(self.context.get('request'), instance)

            return instance