Untitled
raw download clone
TEXT
views 13
,
size 1488 b
class UserCreateSerializer(serializers.ModelSerializer):
    password_confirm = serializers.CharField(style={"input_type": "password"}, write_only=True)
    images = PsychologistImageSerializer(many=True)

    class Meta:
        model = CustomUser
        fields = '__all__'

    def validate(self, data):
        is_psychologist = data['is_psychologist']
        images = data['images']
        password = data["password"]
        password_confirm = data["password_confirm"]
        if password != password_confirm:
            raise serializers.ValidationError(
                {"password_confirm": "Пароли должны совпадать"})
        if is_psychologist and not images:
            raise serializers.ValidationError(
                {"images": "Необходимо как минимум одно фото"})
        return data

    def create(self, validated_data):
        email = validated_data["email"]
        password = validated_data["password"]
        is_psychologist = validated_data["is_psychologist"]
        images = validated_data["images"]

        user = User.objects.create_user(email=email, password=password, is_psychologist=is_psychologist)
        if is_psychologist:
            psychologist = Psychologist.objects.create(user=user, status="waiting")
            for image in images:
                Image.objects.create(psychologist=psychologist, image=image['image'], name=image['name'])
        return user
close fullscreen
Login or Register to edit or fork this paste. It's free.