Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

icontains in Django QuerySet doesn't give the case insensitive result?

I am using Django1.6, I am trying to use QuerySet filter, but it is behaving strangely.

Here is my code:

test_listing = Test.objects.filter(site__id=_site_id, show_on_site=True)

Then I am trying to get the listing based on the searched text:

test_listing = test_listing.filter(name__icontains = searched_text)

I am using MySQL as database for my project.

When I search for 'foo' it returns a blank list, but when I search for 'FOO', it returns the list of objects where there is an entry of FOOBAR as a name in my Test table.

Why is it behaving so strangely...?

like image 246
Vidya Sagar Avatar asked Feb 20 '14 10:02

Vidya Sagar


1 Answers

this is perhaps due to the mysql-db which you are using, may be your db table (name) column have the sql case-sensitive coollate

ALTER TABLE t1 MODIFY
    col1 VARCHAR(5)
      CHARACTER SET latin1
      COLLATE latin1_swedish_ci;
like image 112
PiyusG Avatar answered Nov 06 '22 11:11

PiyusG