重写get_queryset,但得到空结果集



我有一个图书数据库,我想根据过滤器和关键字进行搜索,所以我在BookSearch视图中重写了get_queryset方法:

class BookSearch(generics.ListAPIView):
serializer_class = ProductDetailViewSerializer
model = ProductDetailView 
def get_queryset(self):
queryset = None
categories = self.kwargs['categories'].rstrip()    
keywords = self.kwargs['keywords'].rstrip()
if isinstance(categories, str) and isinstance(keywords, str):
book_filter = BookFilter(categories)
sql = self.get_sql(categories, keywords, book_filter)
queryset = ProductDetailView.objects.filter(
id__in=RawSQL(sql, book_filter.params)
)
message = f"{queryset.query}"
log_to_file('BookSearch.log', 'BookSearch.get_queryset', message)
return queryset

log_to_file调用记录django使用的查询,我在这里缩写了一下而是如下:

SELECT `jester_productdetailview`.`id`, 
`jester_productdetailview`.`isbn`, 
`jester_productdetailview`.`title`  
FROM `jester_productdetailview` 
WHERE `jester_productdetailview`.`id` IN (
select id from jester_productdetailview   
where ( authors like '%Beatrix%' or 
illustrators like '%Beatrix%' or 
title like '%Beatrix%' ) ) 
ORDER BY `jester_productdetailview`.`title` ASC

如果我在数据库中手动运行该查询,我将得到186行:

'119371','9780723259572','A Beatrix Potter Treasury'
'130754','9780241293348','A Christmas Wish'
'117336','9780241358740','A Pumpkin for Peter' ...

为了获得上面的查询,我通过API调用视图,但是当返回查询集时,没有结果??

http://127.0.0.1:8000/api/分/{"filter"all"/比阿特丽克斯/返回[]

仅在if条件内返回查询集。缺省情况下,该功能为发送。也返回if条件之外的查询集。

相关内容

  • 没有找到相关文章

最新更新