Case insensitive postgres search in ArrayField with django

In the Django documentation for ArrayField , it lists the search containsfield. However, icontainsthere are none icontains(search without a icontainsregister - many other fields have it).

I need a case-insensitive view function for ArrayField, similar to a preexisting containssearch.

+6
source share
2 answers

icontains, iexactApplies only to string:

my_array_field__contains=['H'] 

check if ['H'] is turned on in the field my_array_field

But if you try the following, it will work:

my_array_field__0__icontains='H'

because it checks if the first element contains H or h

+2

icontains, , contains:

queryset = my_model.objects.filter(field_name__icontains='my_substring')

, Postgres ArrayField , my_substring . , :

print(queryset.query)

# raw SQL output:
'SELECT ... WHERE UPPER("my_model"."field_name"::text) LIKE UPPER(%my_substring%)

Postgres 10.1.


, . , , 'my_substring':

field_name__icontains='my_substring'

'my_substring', 'foo_my_substring'. . .

+5

Source: https://habr.com/ru/post/1621915/


All Articles