Created
February 26, 2019 18:09
-
-
Save patrickscottbest/352fe0c211aed54e567062ba0d7f6624 to your computer and use it in GitHub Desktop.
Generate a unique random string in Django without the need for UUID or database locks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## this seems not good, but it's a shortcut | |
## you really shouldn't use this, k? | |
## uses the django make_random_password function | |
def unique_rand(): | |
from django.contrib.auth.models import User | |
from .models import Whatever | |
#while True: | |
for _ in range(10): | |
code = User.objects.make_random_password(length=50) | |
logger.debug ('secret generated code is %s' % code) | |
if not Whatever.objects.filter(secret_unique_storage_filename=code).exists(): | |
return code | |
else: | |
logger.error ("unique_rand here, looks like this aint gonna work. Couldn't find one secret enough...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment