Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to override list() in Django Rest Framework?

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)
like image 883
Solal Avatar asked Nov 04 '25 08:11

Solal


1 Answers

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 )

like image 71
Headmaster Avatar answered Nov 06 '25 03:11

Headmaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!