I'd like to use Django Rest Framework auth but I want to have more than one token for one user. In order to do that I need to implement my own Token model, I found this in Token authentication class:
class TokenAuthentication(BaseAuthentication):
"""
Simple token based authentication.
...
"""
model = Token
"""
A custom token model may be used, but must have the following properties.
* key -- The string identifying the token
* user -- The user to which the token belongs
"""
But I don't have an idea how I can specify this model. Should I subclass TokenAuthentication
?
What that message is saying is that the model
Token
can be swapped out with any other model, as long as it has propertieskey
anduser
. That way, if, for instance, you want a more complicated way to generate token keys, you can define your own model.So if you want a custom Token model, you should do the following:
rest_framework.authtoken.models
. Add whatever custom behavior you want here, but make sure it has akey
property anduser
property.rest_framework.authentication
. Set itsmodel
property to whatever your new Token model is.Define you own authentication method in
settings.py
:In
authentication.py
:In
models.py
: