django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

error_messages on serializer fields should also be used by validators.

Open WhyNotHugo opened this issue 11 years ago • 13 comments

email = models.EmailField(
    unique=True,
    error_messages={
        'unique': 'my message'
    }
)

This does not show the defined error as expected when the field is not unique. The message is still the default: "This field must be unique.".

WhyNotHugo avatar Apr 25 '15 18:04 WhyNotHugo

I see. We automatically add UniqueValidator for model fields that have unique=True here.

@tomchristie I wonder if we should be forwarding error_messages['unique'] to UniqueValidator, which IMHO seems like we should.

@hobarrera A workaround might be to subclass UniqueValidator and change the message there. Then in your serializer manually add your email field with that validator.

jpadilla avatar Apr 26 '15 16:04 jpadilla

@tomchristie I wonder if we should be forwarding error_messages['unique'] to UniqueValidator, which IMHO seems like we should.

I believe that that makes sense and is what would usually be expected by most developers.

@hobarrera A workaround might be to subclass UniqueValidator and change the message there. Then in your serializer manually add your email field with that validator.

Yes, that's what I did. In case anybody else needs a bit more details on this workaround:

class ClientSerializer(serializers.ModelSerializer):

    email = serializers.EmailField(validators=[
        UniqueValidator(
            queryset=Client.objects.all(),
            message="This email is already in use.",
        )]
    )
    # ...

WhyNotHugo avatar Apr 26 '15 19:04 WhyNotHugo

Is the proposed enhancement to take the error messages from what's defined on the model level and forward them on? Edit: I'd be interested in working on this, if so.

ConorMcGee avatar Jun 02 '15 16:06 ConorMcGee

Is the proposed enhancement to take the error messages from what's defined on the model level and forward them on?

Yes, that's what's expected (and it's done for other validators, unless I'm mistaken).

WhyNotHugo avatar Jun 17 '15 07:06 WhyNotHugo

Seems reasonable to me for us to check the validator codes and apply the field error message if one exists, yup. No objection to this from me.

lovelydinosaur avatar Jun 23 '15 12:06 lovelydinosaur

+1 any progress on this one? is this saying we can define error message on the Model level as in https://docs.djangoproject.com/en/1.8/ref/models/fields/#error-messages?

erichaus avatar Aug 26 '15 04:08 erichaus

+1 any progress on this one?

No, if there's progress you'd see it here.

is this saying we can define error message on the Model level

Yup

lovelydinosaur avatar Aug 26 '15 06:08 lovelydinosaur

cool, if you tell me the general idea for how that should work or where I need to make updates I can take a stab at it...

erichaus avatar Aug 26 '15 07:08 erichaus

You'd need to change something here, at least: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/utils/field_mapping.py#L195

That's where a UniqueValidator is automatically added to the serializer for fields with unique constraints. It takes an optional message parameter.

I'm not quite sure how to get the message off of the model field, though.

stianjensen avatar Aug 26 '15 09:08 stianjensen

I'm not quite sure how to get the message off of the model field, though.

Error messages for a specific field are available as field.error_messages. The unique message uses the key unique.

kevin-brown avatar Aug 26 '15 13:08 kevin-brown

@tomchristie I wonder if we should be forwarding error_messages['unique'] to UniqueValidator, which IMHO seems like we should.

I believe that that makes sense and is what would usually be expected by most developers.

@hobarrera A workaround might be to subclass UniqueValidator and change the message there. Then in your serializer manually add your email field with that validator.

Yes, that's what I did. In case anybody else needs a bit more details on this workaround:

class ClientSerializer(serializers.ModelSerializer):

    email = serializers.EmailField(validators=[
        UniqueValidator(
            queryset=Client.objects.all(),
            message="This email is already in use.",
        )]
    )
    # ...

you should have indicated what file you added this to. It isn't really helpful just like this

elizabethadegbaju avatar Sep 21 '18 09:09 elizabethadegbaju

@iampicca I added this to my serializers, which are in my serializers.py file. I think that if you're writing a project using DRF, it should be pretty obvious is which file your serializers are.

WhyNotHugo avatar Sep 24 '18 15:09 WhyNotHugo

from rest_framework.validators import UniqueValidator

email = serializers.EmailField(validators=[ UniqueValidator( queryset=User.objects.all(), message="This email is already register by user.", )] ) This will work for you...###

nimeshtntra avatar Aug 20 '21 06:08 nimeshtntra