# 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 = { 'available_for': 'property__houselet_available_for', 'available_to_move_in': 'property_available_from', 'district': 'property__district', 'furnished': 'property__houselet__furniture_option', 'max_price': 'property__houselet__rent_price__lte', 'min_price': 'property__houselet__rent_price__gte', 'property_type': 'property__property_type' } # 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.clean_data[k]}) for k in qdict.keys() if form.clean_data.get(k, None)] search_results = Ad.objects.select_related().filter(*q_objs)