Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvanced search for a specific Django model
    text
    copied!<p>I'm aware of full text search applications like <a href="http://code.google.com/p/django-solr-search/" rel="nofollow noreferrer">Django Solr</a> and solango, etc.</p> <p>What I'm looking to built is more akin to an advanced search for a real estate site. E.g they can choose location, price, etc. similar to <a href="http://www.viewr.com" rel="nofollow noreferrer">www.viewr.com</a> advanced search.</p> <p>What I have done so far is this, under the models custom manager:</p> <pre><code>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 </code></pre> <p>Right now I'm not getting very predictable results. Is there something I might be doing wrong? Is there a better way of doing the various OR searches/joins?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload