Django Tasks Special Characters

I am working on a location from google maps and am using django to. My question is:

I have a line in request.GET['descricao']

if say it contains "Via rapida".

In my database, I have store = "Via Rápida", I do:

local = Local.objects.filter(name__icontains=request.GET['descricao'])

      

so that I can get anything, like "Via Rapida", but the result that "Via rápida" never gets matched in the query (ASCI character maybe?)

what if the string "Via rapida" matches "via rápida" and "via rapida"? Ordinary expressions? How?

0


source to share


2 answers


I think the best way is to use full text search , here is the Full Text search engine list that can be used with django:

And we shouldn't forget Haystack , which is a proxy server that can use another search engine like solr, whoosh ...;



You can also use full text database search without using a third library like this .

EDIT: From the OP's comment, the QuerySet API offers a full text search feature that only works with MySQL, checking here .

+2


source


If you are interested in non-ascii search but icontains

suits your needs, I would use a simpler approach to normalize only Unicode characters with accents (accents).

# -*- coding: utf-8 -*-
import unicodedata
def strip_accents( text, encoding='ASCII'):
    return ''.join(
        (c for c in unicodedata.normalize('NFD', unicode(text))
        if unicodedata.category(c) != 'Mn') )

if __name__ == "__main__":
    print strip_accents( u"Corrão quê a polícia vem aí! Será ¿")
    print strip_accents( u"Wie spricht man diessen Wörter aus?" )

      



This obviously means that you have to separate the emphasis on save / update operations to maintain consistency. Sadly, your text will lose all accents ... forever!

+1


source







All Articles