Django custom field - automatically adds COLLATE to the request

I am trying to create a custom field that automatically adds COLLATE information to the WHERE part of an SQL query:

class IgnoreDiacriticsField(models.TextField):

    def get_prep_lookup(self, lookup_type, value):
        if lookup_type == 'exact':
            return ' "' + self.get_prep_value(value) + '" COLLATE utf8_general_ci'

when I execute a query like this:

result = ModelClass.objects.filter(field='value')

then nothing is found even if the query (print result.query) is valid and matches multiple lines. Am I doing something wrong?

The reason I add iformation for sorting is because I want to execute queries in these fields and ignore any diacritics.

+3
source share
1 answer

Are you using MySQL 1.2.1p2 by accident? From the Django documentation

MySQLdb 1.2.1p2, Django CharField unicode utf8_bin. , TextField array.array( Python). Django , , , , , . ​​ MySQLdb 1.2.2, , TextField utf8_bin, 1.2.2 , ( ) , .

0

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


All Articles