I am using Django Restframework 3.3.3, and I am trying to use the generic views, but I was hoping to overwrite the serializer validation error message. I got the following code, which got a "name field cannot be blank" when the name field is not given.
class PositionList(generics.ListCreateAPIView):
"""Get the Position list, or add another Position only when you are admin"""
renderer_classes = ((BrowsableAPIRenderer, JSONRenderer))
permission_classes = (IsAuthenticatedOrReadOnly, IsAdminOrReadOnly,)
queryset = Position.objects.filter()
serializer_class = PositionSerializer
My question is: is there a way to customize the error messages. The following methods dose not work for me: (1). Overwrite the init method in the serializer class:
def __init__(self, *args, **kwargs):
super(UserSerializer, self).__init__(*args, **kwargs)
self.fields['name'].error_messages['required'] = 'My custom required msg'
(2). Give the error message in the serializer class:
class PositionSerializer(serializers.ModelSerializer):
class Meta:
model = Position
fields = ('id', 'name', 'description')
extra_kwargs = {"name": {"required": _("Customized message goes here")}}
Any advises are welcomed, thanks in advance