You can do this with the comp list and the "interested options":
def search_items(request): if 'search_name' in request.GET: interested_params = ('color', 'shape') query_attrs = dict([(param, val) for param, val in request.GET.iteritems() if param in interested_params and val]) items = Items.objects.filter(**query_attrs)
Just for fun (aka not really doing this) you can do it on one line:
def search_items(request): items = Items.objects.filter( **dict([(param, val) for param, val in request.GET.iteritems() if param in ('color', 'shape') and val]) ) if 'search_name' in request.GET else None
sdolan
source share