[英]Trying to override a get_queryset in a view
class OwnerList(generics.ListAPIView):
serializer_class = OwnerDetailSerializer
# queryset = Person.objects.filter(customuser__userrole__role__name='OWNER').distinct()
permission_classes = [IsAuthenticated]
filter_backends = [DjangoFilterBackend]
def get_queryset(self):
return super(OwnerList, self).get_queryset()
I have this simple view and i am trying to over ride the get_queryset.我有这个简单的视图,我正试图超越 get_queryset。 The issue is that when this view is used i get:
问题是,当使用此视图时,我得到:
return super(OwnerList, self).get_queryset()
返回 super(OwnerList, self).get_queryset()
File "C:\Users\kdalipaj\PycharmProjects\LTC SYSTEM\venv\lib\site-packages\rest_framework\generics.py", line 63, in get_queryset assert self.queryset is not None, (
文件“C:\Users\kdalipaj\PycharmProjects\LTC SYSTEM\venv\lib\site-packages\rest_framework\generics.py”,第 63 行,在 get_queryset 中断言 self.queryset 不是 None,(
AssertionError: 'OwnerList' should either include a
queryset
attribute, or override theget_queryset()
method.AssertionError: '
queryset
' 应该包含查询集属性,或者覆盖get_queryset()
方法。
Why is this happening?为什么会这样?
Your code is:你的代码是:
return super(OwnerList, self).get_queryset()
it means: "Call method get_queryset of ListAPIView.这意味着:“调用 ListAPIView 的方法 get_queryset。
ListAPIView doesn't have get_queryset method itself but GenericAPIView (which is a parent of ListAPIView) has one. ListAPIView 本身没有 get_queryset 方法,但 GenericAPIView(ListAPIView 的父级)有一个。
And get_queryset of the GenericAPIView doing simple thing: it throwing assertion. GenericAPIView 的 get_queryset 做的事情很简单:它抛出断言。
So, you shouldn't call所以,你不应该打电话
return super(OwnerList, self).get_queryset()
Just create your own queryset and return it from the function. For example:只需创建您自己的查询集并从 function 返回它。例如:
return Owner.objects
Another solution is to set queryset
variable at a class-level:另一种解决方案是在类级别设置查询
queryset
变量:
class OwnerList(generics.ListAPIView):
...
queryset = Owner.objects
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.