在进行搜索功能实现的时候,我用set将building_result和coperation_result两个搜索结果集合在了一起,然后在计算集合的数量时候,报错
AttributeError at /search/
'set' object has no attribute 'count'
from django.shortcuts import render
from myadmin.models import Building
from myadmin.models import Corporation
from django.db.models import Q
# Create your views here.
def index(request):
''' 项目前段,企业搜索'''
return render(request,"web/index.html")
def search(request,pIndex=1):
search_word = request.GET.get('wd', '') # 获取到wd这个参数
building_result = Building.objects.filter(Q(name__contains=search_word))# __contains= 部分匹配
coperation_result = Corporation.objects.filter(Q(name__contains=search_word))
tmp_content_list = set()
if building_result:
tmp_content_list = set(building_result)
# 最后将符合条件的文章标题模型和文章内容模型合并返回
info_list = set()
if coperation_result:
info_list = set(coperation_result)
# 在从文章标题模型中查找出最终汇总的模型返回
search_result = info_list.union(tmp_content_list)
context = {'search_word':search_word, 'search_result':search_result, 'search_result_count':search_result.count()}
return render(request, 'web/search.html',context)