How can I change existing token in the authtoken of django-rest-framework?

Viewed 7295

Saving fails:

from rest_framework.authtoken.models import Token

token = Token.objects.get(user=user1)
token.key = '1'
token.save()

gives

IntegrityError: duplicate key value violates unique constraint "authtoken_token_user_id_key"

3 Answers

This sample code work for me and update token in database:

t = Token.objects.filter(user=user1)
new_key = t[0].generate_key()
t.update(key=new_key)
Related