I have a form which sends some parameters to a view where I use this parameters as a filter in a query. The search parameters are not obligatory and when a field of the form is left blank, I do not use it as a filter criteria.
I was trying to create the string for the query but get an error: "too many values to unpack"
Any Ideas of what could be happening? Also any ideas of a simpler way to do it would also be appreciated!
My view is like this (in the dict busca, the keys are form names and values database fields as django model)
def index(request):
a={}
busca={'marca':'brand = ', 'modelo':'model = ', 'tamano_min':'size >= '}
for i in request.POST:
if i in ['marca', 'modelo', 'tamano_min']:
valor=request.POST.get(i)
a[i]=str.join(busca[i], valor)
query=list(a.values)
query=' and '.join(query)
latest_products = products.objects.filter(query, )
template = loader.get_template('search/index.html')
context = {'latest_products': latest_products}
return render(request, 'search/index.html', context)