1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] validation process in django serializer

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    I have a question

    class RegisterSerializer(ModelSerializer):
    username = CharField(required=True)

    class Meta:
    model = User
    fields = ['username', 'password', 'email']

    def create(self, validated_data):
    if User.objects.filter(username=validated_data['username']).exists():
    raise ValidationError('ushbu username mavjud!')
    if User.objects.filter(email=validated_data['email']).exists():
    raise ValidationError("ushbu email mavjud!")
    user = User.objects.create_user(
    username=validated_data['username'],
    password=validated_data['password'],
    email=validated_data['email']
    )
    return user


    this message given for the first username in this serializer:

    if User.objects.filter(username=validated_data['username']).exists():
    raise ValidationError('ushbu username mavjud!')


    it works, but the validation message for the email does not appear:

    if User.objects.filter(email=validated_data['email']).exists():
    raise ValidationError("ushbu email mavjud!")


    instead, drf from its default message:

    "email": [
    "user with this email already exists."
    ]


    Why is it being used?

    i expected validation error given inside serializer

    Continue reading...

Compartilhe esta Página