My question is all in the title.
I am using DRF mixins and I would like to know if I can override method list() in class ListModelMixin. I need to add some computed information to my data.
The code source is as below and you can find it here
class ListModelMixin(object):
"""
List a queryset.
"""
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
Yes, it is a common practice, if you need any different behavior.
This practice is also mentioned in the DRF documentation. So, you can safely do it. (See: examples )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With