# https://gist.github.com/bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed import shortuuid from django.conf import settings DEFAULT_ALPHABET = "0123456789abcdef" def make_unique_code(length=None, namespace=None, alphabet=None): """ Generate a concise, unique identifier code. """ alphabet = ( alphabet or getattr(settings, 'UNIQUE_CODE_ALPHABET', DEFAULT_ALPHABET) ) shortuuid.set_alphabet(alphabet) if namespace: code = shortuuid.uuid(name=namespace) else: code = shortuuid.uuid() return code[:length] if length is not None else code