2023-05-18 20:25:22 +03:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2023-05-21 20:46:07 +03:00
|
|
|
fields = ["username", "email"]
|
2023-05-18 20:25:22 +03:00
|
|
|
|
2023-05-21 20:46:07 +03:00
|
|
|
|
|
|
|
class UserRegisterSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ("username", "email", "password")
|
2023-05-18 20:25:22 +03:00
|
|
|
extra_kwargs = {
|
2023-05-21 20:46:07 +03:00
|
|
|
"password": {"write_only": True},
|
|
|
|
"email": {"required": True},
|
2023-05-18 20:25:22 +03:00
|
|
|
}
|
2023-05-21 20:46:07 +03:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
user = User.objects.create(
|
|
|
|
username=validated_data["username"],
|
|
|
|
email=validated_data["email"],
|
|
|
|
)
|
|
|
|
|
|
|
|
user.set_password(validated_data["password"])
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
return user
|