5👍
djangos orm doesn’t have this behavior out-of-box, but there are several projects that integrate django w/ search services like:
- sphinx (django-sphinx)
- solr, a lightweight version of lucene (djangosearch)
- lucene (django-search-lucene)
I cant speak to how well options #2 and #3 work, but I’ve used django-sphinx quite a lot, and am very happy with the results.
11👍
You could try using python’s difflib module.
>>> from difflib import get_close_matches
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('apple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']
Problem is that to use difflib one must build a list of words from the database. That can be expensive. Maybe if you cache the list of words and only rebuild it once in a while.
Some database systems support a search method to do what you want, like PostgreSQL’s fuzzystrmatch
module. If that is your case you could try calling it.
edit:
For your new “requirement”, well, you are out of luck. No, there is nothing built in inside django’s query language.
0👍
cal_name = request.data['column']['name']
words = []
for col in Column.objects.all():
if cal_name != col.name:
words.append(col.name)
words = difflib.get_close_matches(cal_name, words)
if len(words) > 0 and is_sure != "true":
return Response({
'potential typo': 'Did you mean ' + str(words) + '?',
"note": "If you think you do not have a typo send {'sure' : 'true'} with the data."})
- [Django]-Why Django translation does not use the base language in javascript?
- [Django]-ModuleNotFoundError : no module named : crispy_forms