0

我试图根据 url 中的 kwarg 过滤查询集。我的网址是这样的:

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

我想过滤url中是否存在type参数

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 中的类型指定为“工作流程”,它也没有进入 if 条件。可能出了什么问题?

4

1 回答 1

1

请应用此 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) 
于 2019-02-22T08:58:03.617 回答