Created
March 2, 2012 12:23
-
-
Save qoelet/1958094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# other imports | |
import difflib | |
# So I have a model where I want to search by a text field | |
results = MyModel.objects.filter(some_field__icontains=query) | |
# We may get usually a couple of results at least, but the deal is for a case I'm working on, I only want 1 object | |
# difflib.get_close_matches will text a first arg, then a collection, and n=? for max number of results) | |
result = results.filter(some_field=difflib.get_close_matches(query, [r.some_field for r in results], 1)[0]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment