Issue
I'm trying to change the default error message value of a "non field error" in a serializer. Here's what I've got so far:
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = Item
fields = "__all__"
extra_kwargs = {
"non_field_errors": {
"error_messages": {
"unique": "There exists an item with this name already!"
}
}
}
ItemSerializer
behaves as expected, but the code above has no effect on the output.
Solution
You can change the default error message of non_field_errors
by adding a UniqueTogetherValidator
in validators
(inside your Meta
class) like so:
validators = [
UniqueTogetherValidator(
queryset=Item.objects.all(),
fields=["org", "item_name"],
message="Duplicate items!",
)
]
Answered By - kureal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.