Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - filter( __icontains) doesn't work

Tags:

django

Please, help with trouble:

class SearchResultView(TemplateView):
    template_name = "search_result.html"

    def get_context_data(self, **kwargs):
        context = super(SearchResultView, self).get_context_data(**kwargs)
        location = self.request.GET['location']
        locations_searched = Location.objects.filter(name__icontains=location)
        context['locations_searched'] = locations_searched
        return context

class AdvancedSearchForm(forms.Form):
    location = forms.CharField(label=u"Локация:")

When entering capitalized word in location field I can see result in the view, but when not capitalized - nothing is shown in the view

Thanks!

like image 578
Dennis Avatar asked Oct 16 '25 02:10

Dennis


1 Answers

I am guessing you are using an SQLite database and have some non-ASCII characters in the input. From the SQLite FAQs:

Case-insensitive matching of Unicode characters does not work. The default configuration of SQLite only supports case-insensitive comparisons of ASCII characters. The reason for this is that doing full Unicode case-insensitive comparisons and case conversions requires tables and logic that would nearly double the size of the SQLite library.

Django's documentation also mentions this:

For all SQLite versions, there is some slightly counter-intuitive behavior when attempting to match some types of strings. These are triggered when using the iexact or contains filters in Querysets...

like image 126
solarissmoke Avatar answered Oct 18 '25 16:10

solarissmoke