django-saccess-response
is a Django REST Framework extension that provides a standardized success response format for API views. It simplifies handling both successful and error responses with customizable data structures.
Install the package via pip:
pip install django-saccess-response
In your Django views, use SaccessResponse
to wrap the response data.
from saccess_response.response import SaccessResponse
from rest_framework.views import APIView
class MyView(APIView):
@staticmethod
def get(request):
data = {'key': 'value'}
return SaccessResponse(data)
{
"success": true,
"result": {
"key": "value"
}
}
To handle errors, pass saccess=False
:
from saccess_response.response import SaccessResponse
from rest_framework.views import APIView
class MyView(APIView):
@staticmethod
def get(request):
data = {'key': 'value'}
return SaccessResponse(data, saccess=False)
{
"success": false,
"result": {
"detail": "error"
}
}
You can also customize Django REST Framework's error responses globally by modifying the EXCEPTION_HANDLER
setting in your settings.py
:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'saccess_response.views.saccess_exception_handler'
}
This will format all exceptions using the SaccessResponse
structure.
This package also provides customized DRF generic views and viewsets for standardized response handling.
Standard View | Saccess Equivalent |
---|---|
CreateAPIView |
SaccessCreateAPIView |
RetrieveAPIView |
SaccessRetrieveAPIView |
UpdateAPIView |
SaccessUpdateAPIView |
DestroyAPIView |
SaccessDestroyAPIView |
ListAPIView |
SaccessListAPIView |
RetrieveUpdateAPIView |
SaccessRetrieveUpdateAPIView |
RetrieveDestroyAPIView |
SaccessRetrieveDestroyAPIView |
RetrieveUpdateDestroyAPIView |
SaccessRetrieveUpdateDestroyAPIView |
ModelViewSet |
SaccessModelViewSet |
ReadOnlyModelViewSet |
SaccessReadOnlyModelViewSet |
These classes behave like their DRF counterparts but automatically format responses using SaccessResponse
.
This project is licensed under the MIT License. See the LICENSE file for details.