mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 08:14:16 +03:00
Merge pull request #1283 from OddBloke/master
Add required imports to Generating Tokens example, and update with get_user_model()
This commit is contained in:
commit
9c0da46209
|
@ -162,10 +162,12 @@ The `curl` command line tool may be useful for testing token authenticated APIs.
|
||||||
|
|
||||||
If you want every user to have an automatically generated Token, you can simply catch the User's `post_save` signal.
|
If you want every user to have an automatically generated Token, you can simply catch the User's `post_save` signal.
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
|
|
||||||
@receiver(post_save, sender=User)
|
@receiver(post_save, sender=get_user_model())
|
||||||
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
def create_auth_token(sender, instance=None, created=False, **kwargs):
|
||||||
if created:
|
if created:
|
||||||
Token.objects.create(user=instance)
|
Token.objects.create(user=instance)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user