django + django_filter过滤设置无效的问题
整个配置完后,不出错,但是就是没有想要的效果,麻烦大家给看看是咋回事。
如图,本来只查询千字文,结果出来的是整个列表;
以下为代码,专门为搜索写了一个BookSearchView:
模型:
class Book(models.Model):
title = models.CharField(max_length=32,verbose_name="书籍名称")
price = models.IntegerField(verbose_name="价格")
pub_date = models.DateField(verbose_name="出版日期")
bread = models.IntegerField(verbose_name="阅读量")
bcomment = models.IntegerField(verbose_name="评论量")
publish = models.ForeignKey("Publish",on_delete=models.CASCADE,verbose_name="出版社")
authors = models.ManyToManyField("Author",verbose_name="作者")
def __str__(self):
return self.title
实现:
from rest_framework.generics import GenericAPIView,ListCreateAPIView,RetrieveUpdateDestroyAPIView
from django_filters import rest_framework as filters
from django_filters.rest_framework import DjangoFilterBackend
class BookView(ModelViewSet):
# class BookView(ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
class BookSearchView(ListCreateAPIView):
queryset = Book.objects
serializer_class = BookSerializer
filter_backends = [DjangoFilterBackend]
filter_fields = ['title']