功能之模糊搜索

前端页面html

<form action="" class="form-inline pull-right">
    <input type="text" name="query" class="form-control">
    <button class="btn btn-sm btn-primary">搜索</button>
</form>

views函数中

from django.db.models import Q
#类中定义search方法及调用
class ConsultList(View):
    def get(self, request, customer_id):
        q = self.search([])
        if customer_id == '0':
            all_consult = models.ConsultRecord.objects.filter(q, delete_status=False,consultant=request.user_obj).order_by('-date')
        else:
            all_consult = models.ConsultRecord.objects.filter(q, delete_status=False, customer_id=customer_id,consultant=request.user_obj).order_by('-date')
        page = Pagination(request.GET.get('page'), all_consult.count(), request.GET.copy(),10)
        return render(request, 'consultant/consult_list.html',
                      {'all_consult': all_consult[page.start:page.end], 'page_html': page.page_html}, )

def search(self, filed_list): query = self.request.GET.get('query', '') q = Q() q.connector = 'OR' for field in filed_list: # q.children.append(Q(qq__contains=query)) q.children.append(Q(('{}__contains'.format(field), query))) return q

 

posted @ 2019-06-06 16:49  海予心  阅读(150)  评论(0编辑  收藏  举报