I want to create a web server that it used by mobile clients and web clients. Web client developer wants limit offset pagination but mobile client developer wants page number pagination.
In django rest framework seems we can not assign multiple pagination class to one view.
so is there any solution in this situation?
Based on Vikram Ray's answer:
from rest_framework.settings import api_settings
class MultiplePaginationMixin:
    def get_pagination_class(self):
        return self.pagination_class
    
    @property
    def paginator(self):
        """The paginator instance associated with the view, or `None`."""
        pagination_class = self.get_pagination_class()
        if not hasattr(self, '_paginator'):
            if pagination_class is None:
                self._paginator = None
            else:
                self._paginator = pagination_class()
        return self._paginator
Now you can use it without braces:
class MyView(MultiplePaginationMixin, viewsets.ModelViewSet):
    def get_pagination_class(self):
        if some_codition:
            return PageNumberPagination
        return api_settings.DEFAULT_PAGINATION_CLASS
                        simpley use property() function.
class MyView(viewsets.ModelViewSet):
    def get_pagination_class(self):
        if some_codition:
           return PageNumberPagination
        return LimitOffsetPagination
    pagination_class = property(fget=get_pagination_class)
that's all you need.
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