Repository navigation
Custom validation method #9464
Unanswered
mhdemd
asked this question in
Question & Answer
Replies: 1 comment
|
class ProductSerializer(serializers.ModelSerializer):
price = serializers.DecimalField(
max_digits=5,
decimal_places=1,
error_messages={
"max_digits": "Price cannot have more than 5 digits in total.",
"max_decimal_places": "Price cannot have more than 1 decimal place.",
},
)
class Meta:
model = Product
fields = ["id", "name", "price"]
def validate_price(self, value):
if value < 5:
raise serializers.ValidationError("Price must be at least 5.")
return valueKeep |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Describe the bug
When using a custom validation method
validate_pricein 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:
DecimalFieldand a custom validation method for this field.Expected behavior
The custom validation messages defined in the
validate_pricemethod should be shown when the validation fails.Code Snippet
All reactions