Django-过滤器(__icontains)不起作用

Den*_*nis 2 django

请麻烦帮忙:

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"???????:")
Run Code Online (Sandbox Code Playgroud)

在“位置”字段中输入大写单词时,我可以在视图中看到结果,但是如果没有大写,则视图中不会显示任何内容

谢谢!

sol*_*oke 7

我猜您正在使用SQLite数据库,并且输入中包含一些非ASCII字符。从SQLite 常见问题解答

不区分大小写的Unicode字符匹配不起作用。SQLite的默认配置仅支持不区分大小写的ASCII字符比较。这样做的原因是,要进行完整的Unicode不区分大小写的比较和大小写转换,所需的表和逻辑将使SQLite库的大小几乎增加一倍。

Django的文档也提到了这一点:

对于所有SQLite版本,尝试匹配某些类型的字符串时,都有一些违反直觉的行为。这些在使用iexact时触发,或在查询集中包含过滤器...