unholster.django-lookup

Easy SlugField management


License
MIT
Install
pip install unholster.django-lookup==0.1.0

Documentation

django-lookup

Provides lookup tables for Django models including fuzzy-matching helpers and lookup table management features.

Quick start

  1. Add "lookup" to your INSTALLED_APPS :
    INSTALLED_APPS = (
        ...
        'lookup',
    )
  1. Run python manage.py syncdb to create the lookup models
  2. Configure a cache backend (not required)
    LOOKUP_CACHE_BACKEND = "redis://localhost"
  1. Configure domain options (not required)
    LOOKUP_DOMAINS = {
        'stuff': {
            'comparison_function': 'myapp.compare_keys',
        }
    }

Usage example

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