from rest_framework.serializers import ModelSerializer, Serializer from rest_framework import serializers from django.contrib.auth import password_validation from .models import User class UserSerializer(ModelSerializer): class Meta: model = User fields = ( "id", "username", "nickname", ) class UserCreateSerializer(ModelSerializer): class Meta: model = User fields = ( "username", "password", "nickname", ) extra_kwargs = { "password": {"write_only": True}, } def create(self, validated_data): user = User.objects.create_user( username=validated_data["username"], password=validated_data["password"], nickname=validated_data["nickname"], ) return user class PasswordSerializer(Serializer): oldPassword = serializers.CharField(required=True) newPassword = serializers.CharField(required=True) def validate_newPassword(self, value): password_validation.validate_password(value) return value