How does the Tastypie APIKey authentication work? I know there is a signal as mentioned in the documentation:
from django.contrib.auth.models import User
from django.db import models
from tastypie.models import create_api_key
models.signals.post_save.connect(create_api_key, sender=User)
However, when is this called? If I want to give a user their APIkey I know I can find it in the APIKey db that this create_api_key function adds the key into, but where and when do I call this models.signals.post_save function?
Is this just another django model? I think it is?
Is this called everytime a user account is saved?
You can put this in
models.pyfile of the relevant app (such asmain/). Whatpost_save.connect(create_api_key, sender=User)does is that everytime anUserinstance is saved,create_api_key()will be called.Now let’s look into what
create_api_key()does by diving a bit into the source of tastypie:As you can see,
create_api_key()will create a newApiKeyrecord, which will be related to the callingUser. This record will also have a HMAC key when it was saved to theApiKeytable. The key is generated bygenerate_key()function.