Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

Django REST framework: SearchFilter doesn't work with 2 and more values in search bar if they are from the same field

I use SearchFilter to search for therapists:

class TherapistSearchListAPIView(generics.ListAPIView):
permission_classes = [IsAuthenticated]
search_fields = ['first_name',
                 'therapist_profile__skills__title',
                 'therapist_profile__counselling_areas__title']
filter_backends = [filters.SearchFilter]
queryset = Therapist.objects.all()
serializer_class = TherapistSearchSerializer

Generally It works great (searches even when user enters several values from different fields), but there is a problem: When several values are entered in search bar, if among these values 2 or more values are from the same field, even though the request matches with any of therapists, the search returns NULL. Example:

Therapist:

[
  {
     "first_name": "Thomas",
     "skills": [
            {
                "id": "0658374347844fd7b69b3d033e17f9b1",
                "title": "Self-reflection"
            },
            {
                "id": "2c6ab46d4ebb4cb1a46c934f0c30ebbe",
                "title": "Patience"
            },
            {
                "id": "f22c8210dd4d4a3299ea8887c1da7c30",
                "title": "Flexibility"
            }
        ],
        "counselling_areas": [
            {
                "id": "5fb0c57ced4c41129829b3620076dda4",
                "title": "Body dysmorphic disorder"
            }
        ]
]

If I write in search bar "Thomas self-reflection patience", it will return me NULL even though everything matches with the therapist in database. How can I fix this? Is there any solution or I need to write my own search filtering function from the scratch?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...