Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:41:43+00:00 2026-06-10T13:41:43+00:00

Possible Duplicate: filter using Q object with dynamic from user? I am working on

  • 0

Possible Duplicate:
filter using Q object with dynamic from user?

I am working on a filter feature in my app. I send a comma separated string via jquery to Django (within jquery I replace the space with a +, so that it can be sent over the wire).

/?ajax&sales_item=t2,+t1

Now in the view when I retrieve the GET parameters, I can see Django has already replaced the + with a space, which is great. Then I split the keywords by comma and strip the whitespace.

sales_item_raw = request.GET['sales_item']
sales_item_keywords = sales_item_raw.split(',')            

I need first to check if the given names even exist as sales item. I have to use a icontains, hence sales_items can be more than one item.

for item in sales_item_keywords:
        sales_items = profile.company.salesitem_set.filter(item_description__icontains=item.strip())            

Last but not least the queryset is used to filter deals for the given sales_items:

deals_queryset = deals_queryset.filter(sales_item__in=sales_items)

If the user filters for only one keyword that would work fine, however if there are two keywords the sales_items will be obviously overwritten in each loop iteration.

What is the most performant way to solve this? Shall I just append the content of sales_itemsin each iteration to a list outside the loop? And eventually send the new list to the final deals_queryset.filter?

I am not sure if this is a good way to solve this…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:41:44+00:00Added an answer on June 10, 2026 at 1:41 pm

    Use Django’s Q object to create “or” logic in your filter.

    # create a chain of Qs, one for each item, and "or" them together 
    q_filters = Q(item_description__icontains=sales_item_keywords[0].strip())
    for item in sales_item_keywords[1:]:
        q_filters = q_filters | Q(item_description__icontains=item.strip())   
    
    # do a single filter with the chained Qs
    profile.company.salesitem_set.filter(q_filters)
    

    This is ugly code, as I’m not sure how to handle the initial Q elegantly, as I’m not sure what is an “empty” Q to which you can chain all other Qs, including the first one. (I’m guessing you could use Q(pk=pk) but that’s ugly in a different way.)

    EDIT: Ignacio’s link above shows the way, i.e.

    q_filters = reduce(operator.or_, (Q(item_description__icontains=item.strip()) for item in sales_items_keywords))
    profile.company.salesitem_set.filter(q_filters)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Gallery with folder filter I'm using following code to open a gallery
Possible Duplicate: How to filter items from a std::map? What happens if you call
Possible Duplicate: PHP_SELF and XSS Why it's necessary to filter $_SERVER['PHP_SELF'], from e.g.: <form
Possible Duplicate: How do you implement a good profanity filter? I am developing a
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: before_filter :require_owner I am trying to restrict access to certain actions using
Possible Duplicate: Django dynamic model fields Good Morning guys! Scenario is the following. For
Possible Duplicate: Android SMS Receiver not working I am in the beginning stages of
Possible Duplicate: Why does Android app run in small window on tablet emulator? i
Possible Duplicate: Run javascript function when user finishes typing instead of on key up?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.