from Levenshtein import ratio from django.contrib.contenttypes.models import ContentType def get_similar_items(mod, field, string, similarity_ratio=0.6, limit=None): """ Get similar items for a specific model Example usage: similar = get_similar_items(Entry, 'title', 'Espresso is good for you', 0.5, 10) var = Entry.objects.filter(id__in=similar) """ similar_list_id = [] v = ContentType.objects.get_for_model(mod) c = v.model_class() for i in c.objects.all(): if ratio(string, getattr(i,field)) > similarity_ratio: similar_list_id.append(i.id) return similar_list_id[:limit]