[Django]-Django prefetch_related on __str__() method

4👍

Your GenericView or ModelViewSet should have a queryset attribute that defines what set it should work on.

You need to add the prefetch_related / select_related there.

class UserList(generics.ListCreateAPIView):
    queryset = User.objects.all().prefetch_related('groups')
    serializer_class = UserSerializer
    permission_classes = (IsAdminUser,)

Leave a comment