1
votes

I get error when I use the view's function, the bellow is my traceback:

File "/Users/xxx/Desktop/xxx/Project/xxx/qiyun_admin_usermanage/api/views.py", line 40, in <module>
    class UserListAPIView(ListAPIView):
  File "/Users/xxx/Desktop/xxx/Project/xxx/qiyun_admin_usermanage/api/views.py", line 59, in UserListAPIView
    serializer_class = get_serializer_class()
TypeError: get_serializer_class() missing 1 required positional argument: 'self'

My views.py code ie bellow:

class UserListAPIView(ListAPIView):
    """

    """
    queryset = User.objects.filter(is_admin=False, is_staff=False, is_superuser=False).exclude(status=4)

    filter_backends = [SearchFilter, OrderingFilter]
    search_fields = ['username', 'qq', 'email']
    pagination_class = UserPageNumberPagination

    class Meta:
       ordering = ['-id']

    def get_serializer_class(self):
        if self.request.user.is_superuser:
            return UserAdminListSerializer
        else:
            return UserListSerializer

    serializer_class = get_serializer_class()  # this is the line 59

And if I write this line serializer_class = get_serializer_class() to in the front of def get_serializer_class(self): method, I will do not find error.

1

1 Answers

0
votes

You don't need to define the serializer_class attribute since you already overriding the get_serializer_class method. Remove the line 59.

From http://www.django-rest-framework.org/api-guide/generic-views/#attributes

serializer_class - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the get_serializer_class() method.