You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug When using a custom validation method validate_price in a serializer to enforce specific validation rules for a DecimalField, the default validation messages from Django are shown instead of the custom validation messages provided in the method.
To Reproduce Steps to reproduce the behavior:
Create a serializer with a DecimalField and a custom validation method for this field.
Trigger validation errors for the field.
Observe that the default validation messages from Django are shown instead of the custom messages.
Expected behavior The custom validation messages defined in the validate_price method should be shown when the validation fails.
Code Snippet
fromrest_frameworkimportserializersfrom .modelsimportProductclassProductSerializer(serializers.ModelSerializer): classMeta: model=Productfields= ['id', 'name', 'price'] defvalidate_price(self, value): ifvalue<5: raiseserializers.ValidationError("Price must be at least 5.") ifvalue.as_tuple().exponent<-1: raiseserializers.ValidationError("Price cannot have more than 1 decimal places.") iflen(str(value).replace('.', '')) >5: raiseserializers.ValidationError("Price cannot have more than 5 digits in total.") returnvalue# In the code snippet above, when the price is less than 5, the custom error message "Price must be at least 5." is shown correctly.# However, for the other two validation checks, the default Django validation messages are shown instead of the custom messages.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
When using a custom validation method
validate_price
in a serializer to enforce specific validation rules for aDecimalField
, the default validation messages from Django are shown instead of the custom validation messages provided in the method.To Reproduce
Steps to reproduce the behavior:
DecimalField
and a custom validation method for this field.Expected behavior
The custom validation messages defined in the
validate_price
method should be shown when the validation fails.Code Snippet
Beta Was this translation helpful? Give feedback.
All reactions