from rest_framework import serializers
from django.contrib.auth.models import User
from login.models import UserProfile

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        write_only_fields = ('password',)
        read_only_fields = ('id',)


class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('id', 'first_name', 'last_name', 'designation', 'profile_image')
        read_only_fields = ('id',)

from rest_framework import serializers
from django.contrib.auth.models import User
from login.models import UserProfile

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        write_only_fields = ('password',)
        read_only_fields = ('id',)
    
    def create(self, validated_data):
        user = User(email=validated_data['email'], username=validated_data['username'])
        user.set_password(validated_data['password'])
        user.save()
        return user


class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('id', 'first_name', 'last_name', 'designation', 'profile_image')
        read_only_fields = ('id',)

Fix saving passwords in Register API
Fix saving passwords in Register API

override the create method in UserSerializer and set password there

  from rest_framework import serializers
  from django.contrib.auth.models import User
  from login.models import UserProfile
  
  class UserSerializer(serializers.ModelSerializer):
      class Meta:
          model = User
          fields = ('id', 'username', 'email', 'password')
          write_only_fields = ('password',)
          read_only_fields = ('id',)
+     
+     def create(self, validated_data):
+         user = User(email=validated_data['email'], username=validated_data['username'])
+         user.set_password(validated_data['password'])
+         user.save()
+         return user
  
  
  class UserProfileSerializer(serializers.ModelSerializer):
      class Meta:
          model = UserProfile
          fields = ('id', 'first_name', 'last_name', 'designation', 'profile_image')
          read_only_fields = ('id',)
  
Fix saving passwords in Register API
## Code Before:
from rest_framework import serializers
from django.contrib.auth.models import User
from login.models import UserProfile

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        write_only_fields = ('password',)
        read_only_fields = ('id',)


class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('id', 'first_name', 'last_name', 'designation', 'profile_image')
        read_only_fields = ('id',)

## Instruction:
Fix saving passwords in Register API
## Code After:
from rest_framework import serializers
from django.contrib.auth.models import User
from login.models import UserProfile

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password')
        write_only_fields = ('password',)
        read_only_fields = ('id',)
    
    def create(self, validated_data):
        user = User(email=validated_data['email'], username=validated_data['username'])
        user.set_password(validated_data['password'])
        user.save()
        return user


class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        fields = ('id', 'first_name', 'last_name', 'designation', 'profile_image')
        read_only_fields = ('id',)
