I am trying to structure a WHERE question LIKE 'Who%' OR question LIKE 'What%' SQL query from inputs of a POST request.
What is the correct way to do this?
If showall POST value is True then no next filter needs to be matched. All entries returned.
If showall is False then combine the next filters for the OR statement to return entries matching only of the filters provided.
https://docs.djangoproject.com/en/3.1/topics/db/queries/#complex-lookups-with-q-objects
from django.db.models import Q
def getuserlist(request):
if request.method == "POST":
showall = request.POST['showall']
showfl_1 = request.POST['showfl_1']
showfl_2 = request.POST['showfl_2']
if showall == 'true':
filt = Q(listing=any)
elif showfl_1 == 'true':
filt = Q(listing="Filtered1")
elif showfl_2 == 'true':
filt = filt | Q(listing="Filtered2")
searchresult = list(User_data.objects.filter(listing=filt).values_list("Country","gender","listing").order_by('-added_date'))
return searchresult
elif, you never will perform both checks|=operator to combine twoQobjects queries with OR:filter |= Q(attr=value)elifeither.showall,showfl_1andshow_fl2are checked? Then what is the expected result?showallto select all entries, irrespective of next filters. If filters areTruethen applyORon those filters. Entries to be returned for any of those filters matched.