Created
February 27, 2017 18:00
-
-
Save aljiwala/8dc9771bca4ae68397c52494958d3d71 to your computer and use it in GitHub Desktop.
Generate a random username for Django
This file contains hidden or 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
| from random import choice | |
| from string import ascii_lowercase, digits | |
| from django.contrib.auth.models import User | |
| def generate_random_username(length=16, chars=ascii_lowercase+digits, split=4, delimiter='-'): | |
| username = ''.join([choice(chars) for i in xrange(length)]) | |
| if split: | |
| username = delimiter.join([username[start:start+split] for start in range(0, len(username), split)]) | |
| try: | |
| User.objects.get(username=username) | |
| return generate_random_username(length=length, chars=chars, split=split, delimiter=delimiter) | |
| except User.DoesNotExist: | |
| return username |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment