小编5pa*_*1ug的帖子

Django 过滤器不适用于视图集

我一直在尝试使用django-filters,但对象没有被过滤。此外,该权限不适用于partial_update视图

我有一个视图集,它具有基本操作,例如 - list()、retrieve()、destroy()、partial_update() 和其他一些操作,并尝试对其应用过滤器。

经过一些研究,我发现由于我是通过过滤器创建查询集,因此我必须覆盖get_queryset()方法。但是,这似乎也不起作用。是否过滤仅适用于ModelViewSetListApiView

视图集 -

class PostViewSet(viewsets.ViewSet):
"""
The Endpoint to list, retrieve, create and delete Posts.

"""
filter_backends = (DjangoFilterBackend, )
# filterset_class = PostFilter
filter_fields = ('pet_age', 'pet_gender', 'breed')

def get_permissions(self):

    if self.action == 'partial_update' or self.action == 'update':
        permission_classes = [IsPostAuthor, ]

    elif self.action == 'create' or self.action == 'destroy':
        permission_classes = [IsAuthenticated, ]

    else:
        permission_classes = [AllowAny, ]

    return[permission() for permission in permission_classes] …
Run Code Online (Sandbox Code Playgroud)

python django django-filter django-rest-framework django-rest-viewsets

5
推荐指数
1
解决办法
4119
查看次数