samedi 25 avril 2015

Django Q, icontains not working

I've the following simple code for searching my blog:

def SearchView(request):
    template_name = 'search_results.html'

    q = request.GET.get('q', None)
    if q is None or q == '':
        return redirect('/')

    object_list = []

    query = (Q(title__istartswith=q) | Q(title__icontains=q) |
             Q(content__istartswith=q) | Q(content__icontains=q))

    models = ContentType.objects.filter(
        model__in=settings.SEARCHABLE_OBJECTS).all()
    for model in models:
        obj = model.get_all_objects_for_this_type().filter(query).all()
        object_list = chain(object_list, obj)

    objects = list(object_list)

    context = {'posts': objects, 'q': q}
    template = template_name
    return render(request, template, context)

And when the query is Bangkok, I get posts containing Bangkok. But if the query is bangkok, nothing is returned. In django documentation, it says icontains should ignore case, but it doesn't work for me.

I use SQlite, and I really need it as my DB engine.

Any ideas? I will be grateful!

Aucun commentaire:

Enregistrer un commentaire