Reputation: 509
I tried to implement this answer to create a custom base Response
class for all my ModelViewSet
classes. My problem is at the retrieve
function. If the Id
of instance I'm looking for not found, I can't seem to change the Response
with my modified response_format
. It still gives the default response. What should I change my if
condition into?
views.py
:
class ResponseInfo(object):
def __init__(self, **args):
self.response = {
"message": args.get('message', 'success'),
"error": args.get('error', ),
"data": args.get('data', []),
}
class LanguageView(viewsets.ModelViewSet):
def __init__(self, **kwargs):
self.response_format = ResponseInfo().response
super(LanguageView, self).__init__(**kwargs)
permission_classes = [permissions.DjangoModelPermissions]
queryset = Language.objects.all()
serializer_class = LanguageSerializer
def list(self, request, *args, **kwargs):
# call the original 'list' to get the original response.
response_data = super(LanguageView, self).list(request, *args, **kwargs)
# customize the response data.
self.response_format['data'] = response_data.data
if not response_data.data:
self.response_format['message'] = 'List is empty.'
self.response_format['error'] = response_data.status_code
return Response(self.response_format)
def retrieve(self, request, *args, **kwargs):
response_data = super(LanguageView, self).retrieve(request, *args, **kwargs)
self.response_format['data'] = response_data.data
if not response_data.data:
self.response_format['message'] = 'Instance not found.'
self.response_format['error'] = response_data.status_code
return Response(self.response_format)
The JSON response when instance is found. E.g http://127.0.0.1:8000/languages/1/:
{
"message": "success",
"error": null,
"data": {
"id": 1,
"name": "English",
"icon": "http://127.0.0.1:8000/media/language_icons/English.png",
"xml": "http://127.0.0.1:8000/media/-",
"abbreviation": "En"
}
}
The JSON response when instance is not found. E.g URL: http://127.0.0.1:8000/languages/4/:
{
"detail": "Not found."
}
The response I'm hoping for:
{
"message": "Instance not found.",
"error": "HTTP_404_NOT_FOUND",
}
And is it possible to not show the "error"
variable when there's no error? For this case when list is not empty and the searched instance is found.
Upvotes: 2
Views: 1952
Reputation: 47364
You can implement custom exception handler:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None and response.status_code == 404:
response.data = {
"message": "Instance not found.",
"error": "HTTP_404_NOT_FOUND",
}
return response
To apply this handler for you project add it to REST_FRAMEWORK
settings:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
Upvotes: 2