python - Search for Matches by Searching for text in Django model field -


i have model similar this

class item(models.model):     value = models.integerfield()     words = models.charfield(max_length=512, unique=true) 

i able search through objects, , return best matches specific query. 'best match' can defined closest text based result words field, (i can write function returns integer representation of 'closeness' no problem, model search , cacheing problems).

ideally, each time search made, if has been made before, new elements since last time query made should searched (the other results have been cached). can not find django module achieves this.

some info: db: postgres connector: psycopg2

you can use difflib order query, put @ top [-1] best match.

>>> import difflib >>> = ["spam", "eggs", "spammy", "eggy"] >>> b = 'spam' >>> sorted(a, key=lambda x: difflib.sequencematcher(none, x, b).ratio()) ['eggy', 'eggs', 'spammy', 'spam'] 

Comments