我已经在this photo中进行了搜索。它按标题,描述,作者,类别搜索书籍。
我需要根据匹配数对搜索结果进行排序[matchCount
。
我用for循环做了一个丑陋的解决方案。最好的django方法是什么?
条件:
Title/Description
:如果包含书名,则matchCount+=1
Title/Description
:如果包含书籍说明,matchCount+=1
authors
:matchCount+=1
用于书籍的每个匹配作者。categories
存在于搜索参数中:matchcount+=1
对于书籍所属的每个匹配类别。型号:
class Author(SafeDeleteModel):
name = models.CharField(max_length=255)
class Category(SafeDeleteModel):
title = models.CharField(max_length=255)
class Book(SafeDeleteModel):
title = models.CharField(max_length=255)
description = models.CharField(max_length=255)
authors = models.ManyToManyField(Author)
categories = models.ManyToManyField(Category)
查询:
filterObj = dict(request.POST)
orConditions = Q()
filterKeys = filterObj.keys()
bookTitleOrDescription = ''
authorList = categoryList = []
if 'bookTitleOrDescription' in filterKeys:
bookTitleOrDescription = filterObj['bookTitleOrDescription'][0]
orConditions = orConditions | Q(title__icontains=bookTitleOrDescription) | Q(description__icontains=bookTitleOrDescription)
if 'author[]' in filterKeys:
authorList = filterObj['author[]']
orConditions = orConditions | Q(authors__id__in=authorList)
if 'category[]' in filterKeys:
categoryList = filterObj['category[]']
orConditions = orConditions | Q(categories__id__in=categoryList)
searchedObjects = Book.objects.filter(orConditions).distinct().prefetch_related('authors','categories')
排序:
bookList = {}
for book in searchedObjects:
matchCount = 0
if 'bookTitleOrDescription' in filterKeys:
if bookTitleOrDescription in book.title.lower():
matchCount += 1
if bookTitleOrDescription in book.description.lower():
matchCount += 1
if 'author[]' in filterKeys:
for author in book.authors.all():
if str(author.id) in authorList:
matchCount += 1
if 'category[]' in filterKeys:
for category in book.categories.all():
if str(category.id) in categoryList:
matchCount += 1
if matchCount not in bookList.keys():
bookList[matchCount] = []
bookList[matchCount].append(book)
bookList = collections.OrderedDict(sorted(bookList.items(),reverse=True))