Search code examples
djangodjango-authenticationdjango-auth-models

Which is a better alternative to the Django make_random_password Deprecated function?


This aswer suggests the use of the make_random_password, but it is deprecated since Django 4.2. I assume there is a good reason for eliminating this fuction, so what should I use instead?

I searched for alternatives, but could not find any native to Django. I can create my own solution using hashlib, but should I?


Solution

  • It’s very easy to implement this function.

    import secrets 
    
    def make_random_password(length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
        return ''.join(secrets.choice(allowed_chars) for i in range(length))