[Answer]-How to set username in user model to a random string

1πŸ‘

βœ…

From this answer

You can do something like this:

import random
import string

def index(request):
    registerform = UserRegisterForm()
    if request.method == 'POST':
        if 'password' in request.POST:
            registerform = UserRegisterForm(request.POST)    
            if registerform.is_valid():
                username = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10))
                result = registerform.save(commit=False)
                result.set_password(request.POST['password'])
                result.username = username       
                result.save()
                member.user_id = user.id
                member.member_id = result.id
                member.save() 
          ''''
    return render(request,'index.html',{'registerform': registerform,})

0πŸ‘

Please see if you can use this.

import random
l = list(map(chr, range(97, 123))) 
random.shuffle(l)    
''.join(l)[0:7] 
πŸ‘€user2598057

Leave a comment