from rest_framework import serializers from django.contrib.auth.models import User class UserSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) def create(self, validated_data): # Create the user instance user = User.objects.create_user( username=validated_data['username'], email=validated_data.get('email', ''), # Default to empty string if email is not provided password=validated_data['password'], first_name=validated_data.get('first_name', ''), # Default to empty string if first_name is not provided last_name=validated_data.get('last_name', '') # Default to empty string if last_name is not provided ) return user class Meta: model = User fields = ('id', 'username', 'password', 'first_name', 'last_name', 'email')