Django PasswordResetTokenGenerator

What is the difference between using

from django.contrib.auth.tokens import PasswordResetTokenGenerator
from six import text_type

class TokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            text_type(user.pk) + text_type(timestamp) +
            text_type(user.is_active)
        )

account_activation_token = TokenGenerator()

AND

from django.contrib.auth.tokens import PasswordResetTokenGenerator
account_activation_token = PasswordResetTokenGenerator()

Initializing the PasswordResetTokenGenerator seems fine, without the need to override the _make_hash_value method.

Reference: django/django/contrib/auth/tokens.py at main · django/django · GitHub

When in doubt, take a look at the source. The comments in that function describe what it’s doing and why, and will help you understand the difference between the two.

1 Like