2

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
9
  • 1
    By using elif, you never will perform both checks Commented Dec 31, 2020 at 9:17
  • You can use the |= operator to combine two Q objects queries with OR: filter |= Q(attr=value) Commented Dec 31, 2020 at 9:18
  • @WillemVanOnsem Will fix that. It didn't work on the first elif either. Commented Dec 31, 2020 at 9:18
  • It is furthermore not entirely clear to me how the filtering should behave: if none of showall, showfl_1 and show_fl2 are checked? Then what is the expected result? Commented Dec 31, 2020 at 9:19
  • @WillemVanOnsem showall to select all entries, irrespective of next filters. If filters are True then apply OR on those filters. Entries to be returned for any of those filters matched. Commented Dec 31, 2020 at 9:25

2 Answers 2

1

You can construct a Q object that is a disjunction of the options:

from django.http import JsonResponse

if showall != 'true':
    filters = []
    if showfl_1 == 'true':
        filters.append(('listing', 'filtered1'))
    if showfl_1 == 'true':
        filters.append(('listing', 'filtered2'))
    if not filters:
        searchresult = User_data.objects.none()
    else:
        searchresult = Q(*filters, _connector=Q.OR)
else:
    searchresult = User_data.objects.all()

searchresult = list(searchresult.values_list(
    'Country','gender','listing'
).order_by('-added_date'))
return JsonResponse({'data': searchresult})
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. Let me try.
0

You may try something like this

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']
        c = {}
        if showall:
            c['listing']= Q(listing=any)
        elif showfl_1:
            c['listing']= Q(listing="Filtered1")
        elif showfl_2:
            c['listing'] = Q(listing="Filtered2")

        searchresult = list(User_data.objects.filter(**c).values_list("Country","gender","listing").order_by('-added_date'))
   return searchresult

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.