简体   繁体   中英

Search filter django drf

i want to get image as multiple search keyword at once:

views.py:

class ImageSearchView(generics.ListAPIView):
      authentication_classes = []
      permission_classes = []
      queryset = Image.objects.all()
      serializer_class = ImageSearchSerializer
      filter_backends = (filters.SearchFilter,)
      search_fields = ['image_keyword']

models.py:

class Image(models.Model):
      license_type = (
         ('Royalty-Free','Royalty-Free'),
         ('Rights-Managed','Rights-Managed')
       )
      image_number = models.CharField(default=random_image_number,max_length=12,unique=True)
      title = models.CharField(default=random_image_number,max_length = 100)
      image = models.ImageField(upload_to = 'image' , default = 'demo/demo.png')
      thumbnail = models.ImageField(upload_to='thumbs', blank=True, null=True)
      category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.CASCADE)
      shoot = models.ForeignKey(ImageShoot, on_delete=models.CASCADE, related_name='Image', null=True,blank=True)
      image_keyword = models.TextField(max_length=1000)


      def __str__(self):
         return self.title

urls.py:

    path('image_search/',views.ImageSearchView.as_view(), name = 'image_search'),

when i make a request from postman:

localhost:8000/api/image_search?search=boxing cricket kohli marykom

if i put & between them then it doesn't work also:

在此处输入图片说明 i want to get every image which has keyword like any of the search parameter

I suggest you to use django-filter for this case, you can read more here:

https://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend

https://django-filter.readthedocs.io/en/latest/guide/rest_framework.html

If you don't want install anything - you can override get_queryset method of your ListAPIView . You need to get all the query-params you need and return the queryset filtered by them. In this case your code will be like this:

def get_queryset(self, request, *args, **kwargs):
    queryset = Image.objects.all()
    keywords = self.request.query_params.get('search')
    if keywords:
        queryset = queryset.filter(image_keyword__in=keywords.split(','))
    return queryset

In this case make sure to remove filter_backends , search_fields and queryset fields from ImageSearchView class

The django-rest-framework-filters package works together with the DjangoFilterBackend class, and allows you to easily create filters across relationships, or create multiple filter lookup types for a given field.

read there

that part of docs for you i think

title__startswith=Who, title__startswith=What
title__startswith%3DWho, title__startswith%3DWhat
(title__startswith%3DWho) | (title__startswith%3DWhat)
%28title__startswith%253DWho%29%20%7C%20%28title__startswith%253DWhat%29
filters=%28title__startswith%253DWho%29%20%7C%20%28title__startswith%253DWhat%29

Have you tried specifying the search param multiple times like this? In this case, it should 'search' query param should end up as a list.

localhost:8000/api/image_search?search=&search=boxing&search=cricket&search=kohli&search=marykom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM