Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValidationError with Custom pagination in ListAPIView

I am trying to set error message in my ListAPiview where if a user tries to access data other than Pool Operator he should get a permisiion denied error

Views.py

class ProductListAPIView(generics.ListAPIView):
    serializer_class = ProductSerializer
    pagination_class = StandardResultsSetPagination
    permission_classes = (permissions.IsAuthenticated, )

    def get_queryset(self):
        company = self.request.GET['company']
        view = self.request.GET['view']
        if view=='Pool Operator':
            emp = list(Employee.objects.filter(company=company).values_list('pk', flat=True))
            queryset = Product.objects.filter(owner__in=emp).order_by('-pk')
            return queryset
        else:
            return ValidationError(
                {'permission denied': "Can't see user's Products"}
            )

But when I run this I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

object of type ‘ValidationError’ has no len()

What needs to be changed to show a successful error message?

>Solution :

You need to use raise instead of return.

So, code will be

raise ValidationError(
                {'permission denied': "Can't see user's Products"}
            )

You can also use custom exception handler in django settings.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading