Django – 500 best messages with api view DRF

So I want all my APIs to return 500 nice messages if something is wrong, ideally, in my current configuration of the Django Python 3.7 application using DRF (Django framework).

To solve this problem, I tried to make a decorator (crushing the decorator of api_view would probably be better).
But that does not work as expected.

For example, if I put an intentional error of 0/0, I get:
TypeError: Object of type ZeroDivisionError

Here are my functions

@api_view (["POST"])
@ view_500_wrapper
def foobar (Request: Request, foo_id: UUID):
if request.method == "POST":
data = dostuff ()
0/0
return Reply (data, status = 201)

def view_500_wrapper (f):
def wrapper (* args, ** kw):
try:
return f (* args, ** kw)
except exception as inst:
exception_dict = get_exception_dict (f, inst, * args, ** kw)
return Response (exception_dict, status = status.HTTP_500_INTERNAL_SERVER_ERROR)

return the package

I have the impression that it is a combination of the order of execution between the two decorators, their entries and return objects that are incorrect.

Here is the full error message

Fault
Traceback (most recent call last):
"/Opt/project/middletier/tests/test_views.py" file, line 660, in foobar
format = & # 39; json & # 39;
"/Usr/local/lib/python3.6/dist-packages/rest_framework/test.py" file, line 300, in post
path, data = data, format = format, content_type = content_type, ** extra)
"/Usr/local/lib/python3.6/dist-packages/rest_framework/test.py" file, line 213, in post
return self.generic ("POST", path, data, content_type, ** extra)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/test.py", line 238, in generic
method, path, data, type of content, secure, ** extra)
File "/usr/local/lib/python3.6/dist-packages/django/test/client.py", line 414, in generic
return to self-request (** r)
"/Usr/local/lib/python3.6/dist-packages/rest_framework/test.py" file, line 289, on demand
super return (APIClient, auto). request (** kwargs)
"/Usr/local/lib/python3.6/dist-packages/rest_framework/test.py" file, line 241, on demand
request = super (APIRequestFactory, self) .request (** kwargs)
"/Usr/local/lib/python3.6/dist-packages/django/test/client.py" file, line 495, in the request
increase exc_value
"/Usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py" file, line 34, inside
response = get_response (request)
"/Usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" file, line 156, in _get_response
response = self.process_exception_by_middleware (e, query)
"/Usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" file, line 154, in _get_response
response = response.render ()
"/Usr/local/lib/python3.6/dist-packages/django/template/response.py" file, line 106, in the render
self.content = self.rendered_content
"/Usr/local/lib/python3.6/dist-packages/rest_framework/response.py" file, line 72, in render_content
ret = renderer.render (self.data, shared_media_type, context)
"/Usr/local/lib/python3.6/dist-packages/rest_framework/renderers.py" file, line 106, in the render
allow_nan = not self.strict, separators = separators
"/Usr/local/lib/python3.6/dist-packages/rest_framework/utils/json.py" file, line 28, in dumps
returns json.dumps (* args, ** kwargs)
"/Usr/lib/python3.6/json/__init__.py" file, line 238, in the backups
** kw) .encode (obj)
"/Usr/lib/python3.6/json/encoder.py" file, line 199, in code
pieces = self.iterencode (o, _one_shot = True)
"/Usr/lib/python3.6/json/encoder.py" file, line 257, in iterencode
returns _iterencode (o, 0)
"/Usr/local/lib/python3.6/dist-packages/rest_framework/utils/encoders.py" file, line 68, default
super return (JSONEncoder, self) .default (obj)
"/Usr/lib/python3.6/json/encoder.py" file, line 180, default
o .__ class __.__ name__)
TypeError: The object of type 'ZeroDivisionError & # 39; is not serializable JSON

Optional pk argument for CREATE in DRF ModelViewSet from Django

I have a standard ModelViewSet for one of my models, but I want to have the option to specify a specific PK to create a new instance. For example. if I post:

{
"name name"
}

He would get a random pk. But if I posted:

{
"id": "123",
"name name"
}

I want it to have the specified pk (id).

Similar to this person, what I've done is to add the login field to my ModelSerializer like this:

ConversationSerializer class (serializers.ModelSerializer):
id = serializers.CharField () # Instead of serializer.ReadOnlyField ()

Meta class:
model = Conversation
fields = __ all __ & # 39; __

Although it works for the create method it poses problems for the update and partial update those, where login is now a required argument as a query string parameter and in the request body like this (from the documentation):

update
PUT / conversations / {id} /
Update the existing conversation.

Path parameters
The following parameters must be included in the path of the URL.

Parameter Description
id (required) A unique value that identifies this conversation.

Body of the request
The body of the request must be an object coded "application / json", containing the following elements.

Parameter Description
Username (Required)
access token
User name
password
app_user_id
first name    

How can I solve this problem so that the login parameter is only optional for the create method, but all other methods (list, read, …) remain exactly the same as in the default case?