django-rest-frameworkdjango-urlsdjango-polymorphic

django filter queryset based on kwarg in url


Im trying to filter queryset based on kwarg in the url . My url is like:

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

I want to filter if the type parameter exists in the 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

At the moment ,even if i have given the type in url as "workflow", it is not entering the if condition.What could be wrong?


Solution

  • Please apply this http://localhost:8000/notification/all/?type=workflow

    remove the " from url

    if type is not workflow simply raise error

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