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

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:

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 = [
        "id",
        "bar",
        "baz",
    ]

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)?

django

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Main Pros and Cons of Django As A Web Framework for Python Developers

India's best Institute for Django Online Training Course & Certification. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

django-allauth Django REST Framework |teratail

Djangoアプリを、当初は Django REST Framework (DRF) を使わずに作成しました。(認証パッケージは、 django-allauthを使用)アプリリリース後に、DRFも追加で導入して、DRFで作成したAPIでのデ

Django CRUD Application — PostgreSQL

In this blog, let’s see what is CRUD and how to perform CRUD with Django. Also, visit my previous blogs if you have any problem with connecting Django and Databases. In this blog, I am performing CRUD functionality with PostgreSQL.

Django REST Framework Course for Beginners

In this article we are going to talk about Django REST Framework Course for Beginners, also for more information you can check the complete video for th