0

In my database I have objects with slavic letters (śćę etc.). It is displayed correctly in my database, admin panel and even when I call my object in template. User can search for this object by providing what he wants in text input box: template.html

<input type="text" class="form-control" placeholder="Model" name="searched_model" id="searched_model" value="">

Hovewer when I filter the objects database in my view: views.py

found_products = ProductBase.objects.filter(Q(model__contains=searched_model).order_by('model')

It filters and displays correctly until slavic letter is provided. For example, user wants object with model property as: ObjectŚĆobject If he writes in my search field object it will find and display ObjectŚĆobject. But if he writes objectŚ or even Ś only it will not show anything.

My view returns found_products to template and displays it in table: template.html

{% for item in found_products %}
...
<td>
    {{item.model}}
</td>

I don't know where lies the problem. In settings.py I have correct LANGUAGE_CODE = 'pl-pl'.

EDIT: I did some logging and when I display searched phrase it shows correct string. When I log the queryset after filter it shows empty queryset. So the problem definitely lies in filtering data.

EDIT2: When using other logging method I can see the SELECT query as below when searching for nieokreś:

(0.000) SELECT "zam_produktbase"."id", "zam_produktbase"."ean", "zam_produktbase"."model", "zam_produktbase"."kolor", "zam_produktbase"."symbol", "zam_produktbase"."aktywny", "zam_produktbase"."zam_id" FROM "zam_produktbase" WHERE ("za
m_produktbase"."model" LIKE '%nieokreś%' ESCAPE '\' AND "zam_produktbase"."symbol" LIKE '%%' ESCAPE '\' AND "zam_produktbase"."kolor" LIKE '%%' ESCAPE '\') ORDER BY "zam_produktbase"."model" ASC; args=('%nieokreś%', '%%', '%%')

EDIT3: Looks like the problem is in my sqlite database. When doing SELECT query it doesn't find non english letters.

EDIT4: It was a problem with capital letters, different filtering fixed the problem.

Jorhanc
  • 310
  • 2
  • 13
  • 2
    Which database are you using? – Andy Nov 06 '20 at 07:47
  • It's SQLite3 for now – Jorhanc Nov 06 '20 at 07:51
  • 1
    Sounds like an encoding mismatch. What encoding is the data and what is set for Django? – Klaus D. Nov 06 '20 at 08:06
  • @KlausD. Honestly, I did not interfere in default settings. I don't know where in settings should I look for it. – Jorhanc Nov 06 '20 at 08:12
  • @KlausD. I did some logging and when I display searched phrase it shows correct string. When I log the queryset after `filter` it shows empty queryset. So the problem definitely lies in filtering data. – Jorhanc Nov 06 '20 at 08:20
  • A common misconception in Django is that the chain contain actual items in every step. The whole chain does no more than collecting information and at a later time (usually when you iterate over the queryset) this information is transformed into a query send to the database. You should try to [log the actual queries](https://stackoverflow.com/a/56773783/3929826). – Klaus D. Nov 06 '20 at 09:12
  • @KlausD. Did not know that. Thanks. When using this method I can see > SELECT "zam_produktbase"."id", "zam_produktbase"."ean", > "zam_produktbase"."model", "zam_produktbase"."kolor", > "zam_produktbase"."symbol", "zam_produktbase"."aktywny", > "zam_produktbase"."zam_id" FROM "zam_produktbase" WHERE ("za > m_produktbase"."model" LIKE '%nieokreś%' ESCAPE '\' AND > "zam_produktbase"."symbol" LIKE '%%' ESCAPE '\' AND > "zam_produktbase"."kolor" LIKE '%%' ESCAPE '\') ORDER BY > "zam_produktbase"."model" ASC; args=('%nieokreś%', '%%', '%%') – Jorhanc Nov 06 '20 at 09:18
  • Please add all relevant details to the question itself. – Klaus D. Nov 06 '20 at 09:34

0 Answers0