So, I have the following:
class ObjectViewSet(
mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet
):
"""
REST API endpoints for Objects.
"""
serializer_class = ObjectSerializer
queryset = Object.objects.all()
This returns, say, for a list GET
request:
[
{
"uuid": "787573a2-b4f1-40df-9e3a-8555fd873461",
},
{
"uuid": "2ab56449-1be1-47d7-aceb-a9eaefa49665",
}
]
However, how could I slightly alter this response for mixins to be similar to the following:
{
success: true,
message: 'Some Extra Useful Message',
data: [
{
"uuid": "787573a2-b4f1-40df-9e3a-8555fd873461",
},
{
"uuid": "2ab56449-1be1-47d7-aceb-a9eaefa49665",
}
]
}
Is this possible, or should I just write my own custom endpoint Response()
and not utilise DRF's mixins
capability?
So, essentially, switching the custom:
Response(data, status=None, template_name=None, headers=None, content_type=None)
To:
response = {
'success': true,
'message': 'Some Extra Useful Message',
'data': serializer.data
}
Response(response, status=None, template_name=None, headers=None, content_type=None)