UniqueValidator error message not consistent with Django's #9553
Unanswered
Haj-Res
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The Django Rest Framework relies on Django's default error message for unique field validation, while allowing users the option to customize it. By default, the message is:
"%(model_name)s with this %(field_label)s already exists."
The issue arises when passing the parameters to format the string. Django passes the
model_name
andfield_label
parameters after applying thecapfirst
function to capitalize the first letter. However, the Rest Framework'sget_unique_error_message
function passes these parameters as they are, without modification. This wouldn’t be much of an issue if not for Django's convention, where verbose names for models and fields are lowercase by default and then capitalized programmatically when needed, allowing for more flexibility in messaging.For example, when creating a user in my system, I receive an error message like:
user with this email already exists.
. However, if I used Django’s forms, the error message would be:User with this Email already exists.
.There is no clean solution for capitalizing the error message when needed without changing every model's and field's verbose name to be capitalized, which goes against Django’s convention.
Here is the code from Django that prepares the parameters for formatting the error message before raising it:
And here is the Rest Framework doing the same:
My suggestion is to modify the function used by the Rest Framework so that it calls
capfirst
on both themodel_name
andfield_label
strings. This would ensure the same behavior, with the model name capitalized by default.Beta Was this translation helpful? Give feedback.
All reactions