Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created March 2, 2012 12:23
Show Gist options
  • Save qoelet/1958094 to your computer and use it in GitHub Desktop.
Save qoelet/1958094 to your computer and use it in GitHub Desktop.
# 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