简体   繁体   English

试图覆盖视图中的 get_queryset

[英]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 the get_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.

相关问题 覆盖 get_queryset DetailView - override get_queryset DetailView 将最低价格合并到get_queryset视图中 - Merge lowest price into get_queryset view 在嵌套循环中重写listview中的get_queryset方法 - Override get_queryset method in listview for nested cycles 根据django-guardian权限覆盖get_queryset - Override get_queryset based on django-guardian permissions AssertionError at /api/movies/ 'MovieViewSet' 应该包含一个 `queryset` 属性,或者覆盖 `get_queryset()` 方法 - AssertionError at /api/movies/ 'MovieViewSet' should either include a `queryset` attribute, or override the `get_queryset()` method DRF:“DemoView”应包含“queryset”属性,或覆盖“get_queryset()”方法 - DRF : 'DemoView' should either include a `queryset` attribute, or override the `get_queryset()` method Django rest 框架,应该能够覆盖 get_queryset 而不是定义 queryset 属性吗? - Django rest framework, should be able to override get_queryset and not define queryset attribute? AssertionError:无法在未设置`.queryset`或没有`.get_queryset()方法的视图上应用DjangoModelPermissionsOrAnonReadOnly - AssertionError: Cannot apply DjangoModelPermissionsOrAnonReadOnly on a view that does not set `.queryset` or have a `.get_queryset()` method 无法在没有 `.queryset` 属性或覆盖 `.get_queryset()` 方法的视图上应用 DjangoModelPermissions - Cannot apply DjangoModelPermissions on a view that does not have `.queryset` property or overrides the `.get_queryset()` method Django Rest Framework错误无法在未设置`.queryset`或没有`.get_queryset()`方法的视图上应用DjangoModelPermissions - django rest framework error Cannot apply DjangoModelPermissions on a view that does not set `.queryset` or have a `.get_queryset()` method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM