The scenario is quite straight-forward:
I have a model with some fields that are required. Let's say one of them is a TextField
which can't be blank
.
I also have a ModelSerializer
(Django Rest Framework) that represents that model.
When an empty string is used to set that field through the serializer the error returned comes from the model itself (This field can't be blank
).
I would like to override the error messages only in the serializer level, without the need to explicitly re-specifying every field in the serializer (which I believe is against the DRY principle), having to write a validate_
method for each field and raise my own ValidationError
or having to change the error messages in the Model
level (because sometimes the context of the error message matters to my use-case and the error message should be given accordingly).
In other words, is there a way to override error messages in the serializer level as easy as it is for a ModelForm
:
class MyModelForm(ModelForm):
class Meta:
model = MyModel
error_messages = {"field1": {"required": _("For some reason this is a custom error message overriding the model's default")}}