Rahul Sharma
Rahul Sharma

Reputation: 2495

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:

object of type 'ValidationError' has no len()

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

Upvotes: 0

Views: 126

Answers (1)

Yery cs
Yery cs

Reputation: 322

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.

Upvotes: 1

Related Questions