基于 url 中的 kwarg 的 django 过滤器查询集

django filter queryset based on kwarg in url

我正在尝试根据 url 中的 kwarg 过滤查询集。 我的 url 就像:

http://localhost:8000/notification/all/?type="workflow"

如果类型参数存在于url

中,我想过滤
class NotificationList(generics.ListAPIView):
    model = Notification
    serializer_class = NotificationSerializer

    def get_queryset(self):
        queryset =  Notification.objects.filter(created_for=self.request.user)[:int(settings.NOTIFICATION_PAGE_SIZE)]
        type_param = self.request.query_params.get('type')
        last = self.request.query_params.get('last')
        print("TYPE PARAm", type_param)

        if str(type_param)=="workflow":
                print("TRUEEEEEEEEEEEEEeeeee")
                queryset = Notification.objects.filter(created_for=self.request.user).instance_of(WorkflowNotification)

        if last:
            last_notification_created_on = Notification.objects.get(id=last)
            queryset = queryset.filter(created_on__lt=last_notification_created_on)


        return queryset

目前,即使我将 url 中的类型指定为 "workflow",它也没有输入 if condition.What 可能是错误的?

请申请 http://localhost:8000/notification/all/?type=workflow

从 url

中删除 "

如果类型不是工作流,则简单地引发错误

if type == 'workflow':
  print("TRUEEEEEEEEEEEEEeeeee")
  queryset = ....
else:
    from rest_framework.exceptions import NotFound
    raise NotFound(detail="no notifications", code=404)