Cipher
Cipher

Reputation: 2122

Search ListAPIView in Django Rest API

I want to filter the list view using API View. But it is not filtering according to query. How can i do search query. Can ListAPIView method will be POST

# Jobs API
    class JobsListAPIView(generics.ListAPIView):
        serializer_class = JobListSerialzer
        # pagination_class = ListLimitOffsetPagination
        permission_classes = [UserIsAuthenticated]

        def get_queryset(self, *args, **kwargs):
            # print('self.request.auth', self.request.auth.application.user.userprofile.user_company.id)

            qs = Jobs.objects.exclude(job_is_deleted = True).filter(
                job_company = self.request.auth.application.company
            )

            query = self.request.data.get("query")
            print('query: ', query)

            if query:
                qs = qs.filter(job_status=query)

            return qs

Upvotes: 0

Views: 1453

Answers (3)

aysum
aysum

Reputation: 71

Maybe my solution works for some folks.

this one creates a filter dict from GET parameters and filters the queryset with that. you can send multiple parameters as the same key name, it will use the list of values.

?pk=1&pk=2

it will return two objects if they are exists

def get_queryset(self):
    queryset = self.queryset
    # convert request get to dict first to preserve list of values
    # then iterate over items
    filters = {key + '__in': value for key, value in dict(self.request.GET).items()}

    if isinstance(queryset, QuerySet):
        # Ensure queryset is re-evaluated on each request.
        queryset = queryset.all().filter(**filters)
    return queryset

you can modify the code to ran on a list of values, by splitting the first element of value with ',':

?pk=1,2

Upvotes: 0

rahul.m
rahul.m

Reputation: 5854

Try this

class JobsListAPIView(generics.ListAPIView):
     serializer_class = JobListSerialzer
      # pagination_class = ListLimitOffsetPagination
     permission_classes = [UserIsAuthenticated]

     def get_queryset(self, *args, **kwargs):
            # print('self.request.auth', self.request.auth.application.user.userprofile.user_company.id)

            qs = Jobs.objects.all()
            # if using body data json
            query = self.data.get("query")
            # else
            query = self.request.query_params.get("query")

        if query is not None:
            qs = qs.filter(job_status=query, job_company = self.request.auth.application.company).exclude(job_is_deleted = True)

        return qs

hope it help

for more details follow this link

Upvotes: 2

Sakthi Panneerselvam
Sakthi Panneerselvam

Reputation: 1397

First of all,

You are filtering the data two times. That is not necessary, because it's a waste of time and memory.

class JobsListAPIView(generics.ListAPIView):
    serializer_class = JobListSerialzer
    # pagination_class = ListLimitOffsetPagination
    permission_classes = [UserIsAuthenticated]

    def get_queryset(self, *args, **kwargs):
        # print('self.request.auth', self.request.auth.application.user.userprofile.user_company.id)


        query = self.request.data.get("query")
        print('query: ', query)

        if query:
            qs = Jobs.objects.exclude(job_is_deleted = True).filter(job_status=query, job_company = self.request.auth.application.company)
       else:
            qs = Jobs.objects.exclude(job_is_deleted = True).filter(job_company = self.request.auth.application.company
       )

        return qs

Upvotes: -1

Related Questions