Wildcard searching in Django

10,582

Solution 1

Try using a less strict filter, like __contains:

elif val2=='Name':
        newData = EmployeeDetails.objects.filter(userName__contains=val3)
        flag=True 

Docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#contains

Solution 2

You can use contains query e.g.

Entry.objects.get(headline__contains='Lennon')

See http://docs.djangoproject.com/en/dev/ref/models/querysets/#contains

There are other options too like startswith, endswith, and you can do even regex search on most databases.

Solution 3

Probably field lookups will help you here. It lets to filter by beginning of the word, containing word and so on.

Share:
10,582
rv_k
Author by

rv_k

Software Developer

Updated on June 04, 2022

Comments

  • rv_k
    rv_k almost 2 years

    How can we do a wildcard searching in Django. If i am filtering username from a list in database, how is it possible to display the filtered data with those exact usernames or part of it.?

    def filter(request):
        val3='' 
        if request.GET.has_key('choices'):
            val2=request.GET.get('choices')
        if request.GET.has_key('textField'):
            val3=request.GET.get('textField')
        if request.POST:
            val2=request.POST.get('choices')    
            val3=request.POST.get('textField')
        if val2=='Designation':                
            newData = EmployeeDetails.objects.filter(designation=val3) 
            flag=True 
        elif val2=='Name':
            newData = EmployeeDetails.objects.filter(userName=val3)
            flag=True 
        elif val2=='EmployeeID':
            newData = EmployeeDetails.objects.filter(employeeID=val3)  
            flag=True       
        elif val2=='Project':
            newData = EmployeeDetails.objects.filter(project=val3)   
            flag=True   
        elif val2=='DateOfJoin':
            newData = EmployeeDetails.objects.filter(dateOfJoin=val3) 
            flag=True       
        else:
            return HttpResponseRedirect('/employeeList/')
    

    This is my function for filtering. Now its filtering with exact words. I want to display the userNames even if part of it is given for filtering. Please help me to solve this as i am new with Django

  • Ignas Butėnas
    Ignas Butėnas about 13 years
    for example: newData = EmployeeDetails.objects.filter(designation__contains=val13) or newData = EmployeeDetails.objects.filter(designation__startswith=val3)
  • rv_k
    rv_k about 13 years
    But userName here is a field in table. It gives me this error "FieldError at /filter/ Cannot resolve keyword 'userName_contains' into field."
  • rv_k
    rv_k about 13 years
    when i tried doing like that, it gave me "Field Error'. Here designation, usernames are all field names in db
  • Paul Ankman
    Paul Ankman about 13 years
    Are you using a single underscore between userName and contains? Lookups are prefixed by double underscores. At least that's what I figure from the error you posted.
  • rv_k
    rv_k about 13 years
    well...that was exactly the error..i gave only 1 underscore..thanks a lot for your help..My problem is solved..:)
  • rv_k
    rv_k about 13 years
    hey...i got 1 more doubt with this..is it not case sensitive ?
  • rv_k
    rv_k about 13 years
    when i gave Joh for Johhny it worked..but when i gave joh for Johhny ir didnt work :(
  • Paul Ankman
    Paul Ankman about 13 years
    Read the docs link, it has a lot of examples and more lookup types. For case insensitive filtering you can try: __icontains
  • Nandhini
    Nandhini about 13 years
    @rv_k, other people have given examples based on your code, though I would recommend you to go thru django docs and tutorial first