JInx 123
JInx 123

Reputation: 53

Case Insensitive search in containment filter

I need to perform Case Insensitive search in containment filter instead of using the exact case sensitve matching.

def filter(request, fname, fvalue):

    list = HmsPatient.objects.filter(**{fname:fvalue})
    c = {'list' : list}
    return render_to_response('patient/list.html', c, context_instance=RequestContext(request))

def search(request):
    if request.method == 'POST':
        fname = request.POST.get('filter_name')
    fvalue = request.POST.get('filter_value')
        return filter(request, fname, fvalue);
    else:
        action = HmsPatient().get_search_url()
        form = HmsPatientForm()
        c = {'form': form, 'action' : action}
        c.update(csrf(request))
        return render_to_response('search.html', c, context_instance=RequestContext(request))

Upvotes: 2

Views: 899

Answers (2)

Malik Aqib
Malik Aqib

Reputation: 513

use Double quotes n "icontains" for case insensitive search.

def filter(request, fname, fvalue):

    key = fname + '__icontains'
    list = HmsPatient.objects.filter(**{key: fvalue})
    c = {'list' : list}
    return render_to_response('patient/list.html', c, context_instance=RequestContext(request))

use Double quotes n "contains" for case Sensitive search.

def filter(request, fname, fvalue):
    key = fname + '__contains'
    list = HmsPatient.objects.filter(**{key: fvalue})
    c = {'list' : list}
    return render_to_response('patient/list.html', c, context_instance=RequestContext(request))

in MySQL chosing a collation makes the data values case sensitive, if u dont choose any collation your database will be case insensitive, i.e AbC =abc

Upvotes: 2

Alasdair
Alasdair

Reputation: 308799

Django allows you to do a case insensitive match using iexact. For example:

Blog.objects.get(name__iexact="beatles blog")

In your case, because fname is a variable, you have to append __iexact to the key in your dictionary.

key = fname + '__iexact'
list = HmsPatient.objects.filter(**{key: fvalue})

Django also supports contains and icontains, which you can use in the same way.

Upvotes: 2

Related Questions