Skip to main content

How to change permission based on request.query_params in Django rest frameworks?

I merged together two views that almost does the same thing except the permission, I want to change permission based on: if company id is in the params. If not, it would use a simple IsAuthenticated class and also created a permission for IsCompany.

class FilesView(ListAPIView):
    serializer_class = FileSerializer
    permission_classes = (IsAuthenticated,)
    ...
    
    def get_queryset(self):
        if 'company' in self.request.query_params:
            # In this case I want the IsCompany Permission class
            return get_company_files(self)
        # Otherwise the regular one
        return get_personal_files(self)


source https://stackoverflow.com/questions/70711918/how-to-change-permission-based-on-request-query-params-in-django-rest-frameworks

Comments