Provides lookup tables for Django models including fuzzy-matching helpers and lookup table management features.
- Add "lookup" to your
INSTALLED_APPS
:
INSTALLED_APPS = (
...
'lookup',
)
- Run
python manage.py syncdb
to create the lookup models - Configure a cache backend (not required)
LOOKUP_CACHE_BACKEND = "redis://localhost"
- Configure domain options (not required)
LOOKUP_DOMAINS = {
'stuff': {
'comparison_function': 'myapp.compare_keys',
}
}
import lookup
from myapp.models import Thing
domain = lookup.Domain("stuff")
def make_key(name):
return name.lower().split(" ")[0]
name = "Foo bar"
key = key_from_name(name) # "foo"
try:
# Lookup a Thing by "foo" in the "stuff" domain
obj = domain.get(key)
except KeyError:
# Find similar keys (within a 0.25 threshold)
results = domain.search(key, threshold=0.25)
if len(results) > 0:
# Found a close-enough result, get the first result
best_key, score = results[0]
obj = domain.get(best_key)
else:
# We'll add this Thing since it hasn't been found
thing = Thing(name=name) # "Foo bar"
domain.set(key, thing) # foo -> Thing(Foo bar), in stuff domain