Django rest framework - ValidationError raised in models' save method. How to pass error to http response

I'm using the django rest framework, with a ModelViewset:

class FooViewset(viewsets.ModelViewSet):
    serializer_class = FooSerializer
    queryset = Foo.objects.all()

and a ModelSerializer:

class FooSerializer(serializers.ModelSerializer):
class Meta:
    model = Foo
    fields = [

I also have the model’s save method:

class Foo(models.Model):

def save(self):
if condition:
raise ValidationError(“Illegal parameters”)
return super().save(*args, **kwargs)

When this validation error is triggered, drf, sends a 500 response to the frontend, with no text. How do I get it to instead give a ‘bad request’ response, with the text in the ValidationError (Illegal parameter)?


4 Likes3.20 GEEK