当我使用视图的功能时出现错误,下面是我的追溯:
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'
我的views.py代码即吼:
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
如果我在serializer_class = get_serializer_class()
方法的前面写这行def get_serializer_class(self):
,我会的
找不到错误。
答案 0 :(得分:0)
由于您已经覆盖了serializer_class
方法,因此无需定义get_serializer_class
属性。删除第59行。
来自http://www.django-rest-framework.org/api-guide/generic-views/#attributes
serializer_class - 应该用于验证和反序列化输入以及序列化输出的序列化程序类。通常,您必须设置此属性,或覆盖get_serializer_class()方法。