开发者

Django approximate matching of unicode strings with ascii equivalents

开发者 https://www.devze.com 2022-12-23 06:37 出处:网络
I have the following model and instance: 开发者_如何学Pythonclass Bashable(models.Model): name = models.CharField(max_length=100)

I have the following model and instance:

开发者_如何学Python
class Bashable(models.Model):
    name = models.CharField(max_length=100)

>>> foo = Bashable.objects.create(name=u"piñata")

Now I want to be able to search for objects, but using ascii characters rather than unicode, something like this:

>>> Bashable.objects.filter(name__lookslike="pinata")

Is there a way in Django to do this sort of approximate string matching, using ascii stand-ins for the unicode characters in the database?

Here is a related question, but for Apple's Core Data.


Try searching against a "de-accented" list of names if the initial search fails. Here's a php remove_accents function that could be translated into python easily: remove_accents()

query = u"pinata"
r = Bashable.objects.filter(name=query)
if not r:
    accented = Bashable.objects.values('id', 'name')
    match_ids = [ obj['id'] for obj in accented 
                                if query in remove_accents(obj['name']) ]
    r = Blog.objects.in_bulk(match_ids)
return r

And here's a stackoverflow entry on fuzzy string matching in python: #682367


The first answer to this question shows how to use the strip_accents function which is a python function to achieve what you want. It's not technically part of Django but it is built into Python.

0

精彩评论

暂无评论...
验证码 换一张
取 消