Hello,
I'm a Django newbie, so please forgive me if this is a stupid question. I have a search form that has multiple fields on it. I only wish to filter my queryset by those fields submitted that aren't empty. How do I do that? I'm aware you can chain querysets and Q objects together, but I don't know how to eliminate empty key/value pairs from the chain. I know the following doesn't work, but I thought it might provide insight into what I'm looking for. Thank you.
def art_search(request):
if request.method == 'GET':
form = AdvSearch(request.GET)
if form.is_valid():
art_name = form.cleaned_data['art_name']
art_number = form.cleaned_data['art_number']
artwork = Inventory.objects.filter(
if art_name is not u'':
Q(marketingname=art_name),
if art_number is not u'':
Q(marketingnumber=art_number)
)
return object_list(request, queryset=artwork)
else:
form = AdvSearch()
return render_to_response('art/search.html', {
'form': form,
})