Spaces:
Configuration error
Configuration error
File size: 889 Bytes
52a3fd6 4b98fcb 52a3fd6 4b98fcb 52a3fd6 4b98fcb 52a3fd6 4b98fcb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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')
|