import os def slug_filename(path): """ Returns a callable that can set upload_to to 'path/slug.ext' i is a (model) instance (with a field called 'slug'), f is a filename. >>> upload_to = slug_filename(u"/my/path/") >>> test_model_instance = type("",(),dict(slug=u"slugy"))() >>> upload_to(test_model_instance, u"myfilename.jpg") u"/my/path/slugy.jpg" """ return lambda i,f: os.path.join(path, u''.join((i.slug, os.path.splitext(f)[1])))