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 894641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:25:33+00:00 2026-05-15T14:25:33+00:00

I have a seach by name function, which should return the name of one

  • 0

I have a seach by name function, which should return the name of one person, if the search matches the first name or the last name. The problem is that if i search for strings like ‘firstname lastname’ it doesn’t find the name that matches (guess it’s because of the space between the words) . What should i do for the search to work?
Also, if i want to search with the same search the username (wich is in another table) how can i do it?
Thanks a lot!

my code:

 def search(request):
    query = request.GET.get('q', '')
    if query:
        qset1 = (
            Q(first_name__icontains=query) |
            Q(last_name__icontains=query) 
                 )
        results = UserProfile.objects.filter(qset1).distinct()
    else:
        results = []

    return render_to_response("search/searchName.html", {

    'results': results,     
    'query': query},
    context_instance=RequestContext(request))
  • 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-05-15T14:25:34+00:00Added an answer on May 15, 2026 at 2:25 pm

    Right now your parsing doesn’t split the query up at all, you could do that, loop through the results, and build yourself a Q object to place into your query.

    import operator
    queries = request.GET.get('q').split()
    qset1 =  reduce(operator.__or__, [Q(first_name__icontains=query) | Q(last_name__icontains=query) for query in queries])
    
    results = UserProfile.objects.filter(qset1).distinct()
    

    What this basically will do is split the query up, and loop through each word while doing exactly what you’re already doing. So if they search for “Frank Podolski” it will check that “Frank” is in the firstname, lastname, and then if “Podolski” is also in the firstname, lastname, rather than the query as a whole.

    Of course, there is a lot more you can do to improve search like stemming, removing stop words, etc but that’s out of the context of this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using autocomplete from jquery... and i have this: $('#name_search').autocomplete({ source: search/name.php, open: function(){
I have a problem with my self-made search page. There is 2 tables: **name
I have an an ajax request that looks like this, $('input.fakecheck').click(function(){ alert(deleteing....); $.ajax({ url:/search,
I have a master page that contains a search box which is validated as
I have a search function that queries the database and has ~15 optional parameters.
I have a problem using the Java search function in Eclipse on a particular
I have a file and searching mutiple value i.e I need to search {Name,Class}
I have to search a given file name (let say Keyword) in a directory
I have the following controller: public ActionResult Search(string Name, int? Friend, int? Page) It
Let's say I have a form where users can search for people whose name

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.