diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index f8a871bf1..3fb189251 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -32,6 +32,7 @@ class Token(models.Model): self.key = self.generate_key() return super().save(*args, **kwargs) + @classmethod def generate_key(self): return binascii.hexlify(os.urandom(20)).decode() diff --git a/tests/authentication/test_authentication.py b/tests/authentication/test_authentication.py index 4760ea319..e872fa744 100644 --- a/tests/authentication/test_authentication.py +++ b/tests/authentication/test_authentication.py @@ -397,6 +397,10 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase): key = token.generate_key() assert isinstance(key, str) + def test_generate_key_accessible_as_classmethod(self): + key = self.model.generate_key() + assert isinstance(key, str) + def test_token_login_json(self): """Ensure token login view using JSON POST works.""" client = APIClient(enforce_csrf_checks=True)