M
M
Maxim Zubenko2018-10-15 11:41:19
Django
Maxim Zubenko, 2018-10-15 11:41:19

Why doesn't case-insensitive lookup through Q model work in django?

Everything is simple. In models.py there is a model of products and the search is done only by name:

class Product(models.Model):
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    brand = models.ForeignKey(Brand, on_delete=models.SET(1), default=1)
    title = models.CharField(max_length=120)
    ...

There is a view in views.py for searching (and import from above for it to work):
from django.db.models import Q

def client_search_view(request):
    query = request.GET.get('q')

    if query != None:
        products = Product.objects.filter(Q(title__icontains=query))
        context = {
            'title': 'Результаты поиска по запросу: ' + query,
            'products': products,
        }
    else:
        context = {
            'title': 'Введите строку поиска'
        }
    return render(request, 'client/client_search.html', context=context)

Naturally, there is a client_search.html template for this case, where the search form itself and the results loop are located:
{% extends 'base.html' %}

{% block content %}
    <h3 class="text-center">{{ title }}</h3>
    <form action="{% url 'client_search' %}">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="Вот в этой строке" name="q">
            <button type="submit" class="btn btn-info"><i class="glyphicon glyphicon-search"></i>&nbsp; Искать</button>
        </div>
    </form>
    {% if products %}
          {% for product in products %}
            ....
          {% endfor %}
    {% else %}
        <h3 class="text-center">По заданному запросу изделия отсутсвуют</h3>
    {% endif %}
{% endblock %}

Well, everything seems to be simple.
Yes, by the way the sqlite database.
Django 2
Ubuntu 18
And now title__icontains doesn't work. The behavior is the same, as if it's just title__contains .
Those. for example I enter: radius - finds nothing I
enter: Radius - finds.
At first I thought he couldn't find it because the first word. I tried title_istartswith, but it still does n't seem to take this i into account.
What is the ambush? Maybe he does not know how to work with the Russian language? UTF-8 stands. Maybe sqlite limits? Maybe some nuance?
Sincerely.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question