2009-06-09 40 views
2

我所知道的一样Django Solr和solango全文搜索应用等特定的Django模型

高级搜索我正在寻找内置更像是一家房地产网站的高级搜索。 例如他们可以选择位置,价格等与www.viewr.com高级搜索相似。

我迄今所做的就是这一点,该机型自定义管理器下:

def advanced_search(self, district, location, type, facilities, features, not_permitted): 
     q_objects = [] 
     l_objects = [] 
     t_objects = [] 
     fc_objects = [] 
     ft_objects = [] 
     np_objects = [] 

     if district: 
      if location: 
       for loc in location: 
        l_objects.append(Q(location__exact=loc)) 
      else: 
       l_objects.append(Q(location__district=district)) 


     if type:  
      for ty in type: 
       t_objects.append(Q(listing_type__exact=ty)) 

     if facilities:  
      for fc in facilities: 
       fc_objects.append(Q(property_facilities__exact=fc)) 

     if features: 
      for ft in features: 
       ft_objects.append(Q(property_features__exact=ft)) 
       ft_objects.append(Q(community_features__exact=ft)) 

     if not_permitted:  
      for np in not_permitted: 
       np_objects.append(Q(not_permitted__exact=np))        

     # Start with a bare QuerySet 
     qs = self.get_query_set() 

     if location: 
      qs = qs.filter(reduce(operator.or_, l_objects)) 
     if type: 
      qs = qs.filter(reduce(operator.or_, t_objects)) 
     if facilities: 
      qs = qs.filter(reduce(operator.or_, fc_objects)) 
     if features: 
      qs = qs.filter(reduce(operator.or_, ft_objects)) 
     if not_permitted: 
      qs = qs.filter(reduce(operator.or_, np_objects)) 
     # Use operator's or_ to string together all of your Q objects. 
     return qs 

现在,我没有得到非常明确的结果。有什么我可能做错了吗?有没有更好的方法来做各种OR搜索/连接?

回答

2

我来此方案最接近的是这个职位由Jacob:

http://www.djangosnippets.org/snippets/32/

我所做的是:

def advanced_search(self, form): 
     # It's easier to store a dict of the possible lookups we want, where 
     # the values are the keyword arguments for the actual query. 
     qdict = {'district': 'location__district', 
      'location': 'location', 
      'property_type': 'listing_type', 
      'max_price': 'price__lte', 
      'property_features': 'property_features', 
      'community_features': 'community_features', 
     } 

     # Then we can do this all in one step instead of needing to call 
     # 'filter' and deal with intermediate data structures. 
     q_objs = [Q(**{qdict[k]: form.cleaned_data[k]}) for k in qdict.keys() if form.cleaned_data.get(k, None)]   
     search_results = RealEstateListing.objects.select_related().filter(*q_objs) 
     return search_results 

它工作正常,当我通过单一的选择,但是当通过多项选择时,它会窒息并说:

OperationalError:子查询返回多于1行

2

只是一对夫妇的想法复杂的搜索。

在这种情况下,使用kwargs解包来为过滤方法提供参数非常有用。 像这样可以使代码simplier:

kwargs = {'not_permitted':np,'property_features': ft} 
return qs.filter(**kwargs) 

也许你应该看一看在管理Django的过滤代码。在django管理参数如not_permitted__exact通过GET传递。然后,在过滤之后,整个GET字典可以作为解压缩的kwargs参数传递给过滤方法。当你有很多过滤器选项时,这使事情变得非常简单。

+0

嗯,我错过了你想要的OR事实,而不是AND。 在这种情况下,我认为最好的解决方案是使用诸如Djapian之类的东西,并根据需要为各个字段设置权重。 但正如我所见www.viewr.com高级搜索使用AND方法来筛选它们,而不是OR。 – 2009-06-10 02:57:51

+0

嗨,看到我下面的评论,你的接近于示例片段 – Rasiel 2009-06-10 03:24:01