在Django Rest框架中创建搜索过滤器

时间:2019-02-09 23:57:53

标签: django django-rest-framework

我正在尝试让用户搜索图像,我已经阅读了文档,但我听不懂。我已经安装了Django-filter,但出现此错误

Traceback (most recent call last):
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\generics.py", line 201, in get
    return self.list(request, *args, **kwargs)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\mixins.py", line 47, in list
    serializer = self.get_serializer(queryset, many=True)
  File "C:\Users\dokunbam\Documents\python\instagram-clone\env\lib\site-packages\rest_framework\generics.py", line 112, in get_serializer
    return serializer_class(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'many'

目标是让用户按“标签”,“所有者”或“ image_url”搜索或过滤帖子

评论模型

class Photo(models.Model):
    post = models.TextField(max_length=140, default="")
    image_file = models.ImageField(upload_to='public/images', default='image.png')
    image_url = models.CharField(max_length=140, default='image.png')
    date_uploaded = models.DateTimeField(auto_now=True)
    owner = models.ForeignKey('auth.User', on_delete=models.CASCADE,)
    tags = models.CharField(max_length=10, default='img')

    class Meta:
        ordering = ('date_uploaded',)

    def __str__(self):
        return self.post

view.py

class PhotoSearchList(generics.ListAPIView):
    model = Photo
    serializer_class = PhotoFilterSerializer
    queryset = Photo.objects.all()
    filter_backends = (filters.SearchFilter,)
    filter_fields = ('tags', 'date_uploaded')

Serializer.py

class PhotoFilterSerializer(filters.FilterSet):
    class Meta:
        model = Photo
        fields = ('tags', 'date_uploaded')

urls.py

path('search/', views.PhotoSearchList.as_view()),

在设置中,我按照文档中的说明添加了它

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}

我还在设置中将'django_filters'添加到了INSTALLED_APPS

1 个答案:

答案 0 :(得分:1)

这里的问题是您的PhotoFilterSerializer正在扩展错误的类。它应该扩展FilterSet时扩展ModelSerializer

将其更改为:

from rest_framework import serializers

class PhotoFilterSerializer(serializers.ModelSerializer):
    ...

如果您想使用专用的FilterSet类,则可以创建一个PhotoFilter

from django_filters import rest_framework as filters

class PhotoFilter(filters.FilterSet):

    class Meta:
        model = Photo
        fields = ('tags', 'date_uploaded')

然后使用filterset_class属性在您的视图上分别指定:

class PhotoSearchList(generics.ListAPIView):
    ...
    serializer_class = PhotoFilterSerializer
    filterset_class = PhotoFilter