error_messages on serializer fields should also be used by validators.
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.".
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.
@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.",
)]
)
# ...
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.
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).
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.
+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?
+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
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...
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.
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.
@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
@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.
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...###