Skip to content

Instantly share code, notes, and snippets.

@percyperez
Created June 4, 2013 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save percyperez/5703528 to your computer and use it in GitHub Desktop.
Save percyperez/5703528 to your computer and use it in GitHub Desktop.
Signal function to auto-create ApiKey objects when using tastypie ApiKeyAuthentication.
# models.py
# Work around the ImportError: cannot import name create_api_key
# Issue https://github.com/toastdriven/django-tastypie/issues/937
class User(AbstractUser):
field1
...
@receiver(post_save, sender=User)
def create_user_api_key(sender, **kwargs):
"""
Auto-create ApiKey objects using Tastypie's create_api_key
"""
from tastypie.models import create_api_key
create_api_key(User, **kwargs)
@roboslone
Copy link

roboslone commented Jul 25, 2016

To anybody who got here from Google, here's an example for custom user models:

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_api_key(sender, **kwargs):
    """Auto-create ApiKey objects using Tastypie's create_api_key."""
    from tastypie.models import create_api_key
    create_api_key(sender, **kwargs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment