25 lines
760 B
Python
25 lines
760 B
Python
from django.conf import settings
|
|
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
|
|
from .models import Token
|
|
|
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
|
|
def create_user_token(sender, instance=None, created=False, **kwargs):
|
|
"""
|
|
When a new user is created, this signal will also create a new API token for this user. So every user will have an API token.
|
|
|
|
Arguments
|
|
----------
|
|
sender : sender
|
|
The model that has triggered the signal
|
|
|
|
instance: :attr:`~django.contrib.auth.models.User`
|
|
The newly created user model data
|
|
|
|
created : boolean
|
|
Wether the object was created (True) or updated (False).
|
|
"""
|
|
if created:
|
|
Token.objects.create(user=instance)
|