首页 > 其他分享 >drf 自带BaseFilterBackend

drf 自带BaseFilterBackend

时间:2022-12-10 15:33:26浏览次数:39  
标签:BaseFilterBackend queryset class filter user 自带 id drf

过滤器

from rest_framework.filters import BaseFilterBackend

class MineFilterBackend(BaseFilterBackend):
    def filter_queryset(self, request, queryset, view):
        user_id = request.user['id']
        return queryset.filter(id=user_id)

使用

class UserInfoView(mixin.RetrieveModelMixin, GenericViewSet):
    filter_backends = [MineFilterBackend]
    queryset = models.UserInfo.objects.all()
    serializer_class = account.UserInfoSerializer

标签:BaseFilterBackend,queryset,class,filter,user,自带,id,drf
From: https://www.cnblogs.com/sherwin1995/p/16971645.html

相关文章