Right now, if views encounter an error (invalid fields etc.) they return a Response object with the status code set.
However, instead the view should raise an Exception (e.g. ValidationError). This means that Django rest can handle the exception appropriately, and then set the status code itself. This is useful, for example, if you have a custom exception_handler set.
Right now, if views encounter an error (invalid fields etc.) they return a
Response
object with the status code set.However, instead the view should raise an Exception (e.g.
ValidationError
). This means that Django rest can handle the exception appropriately, and then set the status code itself. This is useful, for example, if you have a customexception_handler
set.In all views, code like this:
Should be changed to: