I'm working on Django==3.2.7, djangorestframework==3.12.4 and django-filter==21.1. and React for the frontend
What I would like to do:
- Return Job Offer objects that contains words from search (Search fields job_title and localistation)
- Remove useless words like ('in', 'a', 'an', 'the', 'at', 'for', 'to') in search
here an example of the url:
{{URL}}/api/v1/job-offers/jobs/?search=Chef in Texas
allowed_methods = ['GET']
What I've done so far in my (ListAPIView):
class JobOfferListAPIView(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated]
queryset = JobOffer.objects.all()
serializer_class = JobOfferSerializer
filter_backends = [filters.SearchFilter, filters.OrderingFilter, DjangoFilterBackend]
search_fields = ['job_title', 'localisation']
ordering_fields = ['user', 'is_active']
filterset_fields = ['user', 'is_active', 'job_title', 'type_of_job', 'start_date', 'salary', 'localisation']
def get_queryset(self, *args, **kwargs):
exclude_words = ['in', 'a', 'an', 'the', 'at', 'for', 'to']
keywords = self.request.GET.get('search').split(' ')
keywords = [keyword for keyword in keywords if keyword not in exclude_words]
if keywords:
for keyword in keywords:
queryset = queryset.filter(
Q(job_title__icontains=keyword) | Q(localisation__icontains=keyword)
)
print(queryset)
return queryset
Problème: When i'm printing queryset i can see filtered Job Offers in terminal but not return in Postman.
Postman Screen
Please share your serializer code.
Meanwhile to test whether the queryset is getting parsed in serializer or not, use
raise Exception(queryset)
or if this is correct thenraise Exception(serializer.data)