搜索记录

时间:2010-04-13 17:39:33

标签: django search

我想出了一个简单的搜索视图来搜索我的应用中的记录。用户只需在搜索框中输入所有参数,然后将所有这些参数与数据库匹配,然后返回结果。其中一个字段是电话号码....现在在数据库中,它以XXX-XXX-XXX格式存储。例如,对于“765-4321”的搜索仅拉出“416-765-4321 ...但是我希望它返回”416-765-4321“和”4167654321“

我的观点如下:

def search(request, page_by=None):    
   query = request.GET.get('q', '')
   if query:
      term_list = query.split(' ')
      q = Q(first_name__icontains=term_list[0]) | 
          Q(last_name__icontains=term_list[0]) | 
          Q(email_address__icontains=term_list[0]) | 
          Q(phone_number__icontains=term_list[0])
      for term in term_list[1:]:
          q.add((Q(first_name__icontains=term) | 
                 Q(last_name__icontains=term) | 
                 Q(email_address__icontains=term) | 
                 Q(phone_number__icontains=term)), q.connector)
      results = Customer.objects.filter(q).distinct()
      all = results.count()
  else:
    results = []

  if 'page_by' in request.GET:
    page_by = int(request.REQUEST['page_by'])
  else:
    page_by = 50

  return render_to_response('customers/customers-all.html', 
                             locals(), context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:2)

一种选择是将精简数字保存在单独的字段中并按其搜索