Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9233173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:29:20+00:00 2026-06-18T06:29:20+00:00

From the tastypie tutorial: from django.contrib.auth.models import User from django.db import models from tastypie.models

  • 0

From the tastypie tutorial:

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)

The tutorial says:

“Tastypie includes a signal function you can use to auto-create ApiKey objects.”

I don’t know where to put this code, help please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:29:21+00:00Added an answer on June 18, 2026 at 6:29 am

    You can put this code in models.py file of the relevant app. Alternatively, you can also place it in signals.py under your app directory — remember to also import signals in your app’s __init__.py file.

    What this code does here is that by using signals, every time a User is created, an HMAC API key will be automatically created for this user [source]:

    class ApiKey(models.Model):
        user = models.OneToOneField(User, related_name='api_key')
        key = models.CharField(max_length=256, blank=True, default='')
        created = models.DateTimeField(default=datetime.datetime.now)
    
        def __unicode__(self):
            return u"%s for %s" % (self.key, self.user)
    
        def save(self, *args, **kwargs):
            if not self.key:
                self.key = self.generate_key()
    
            return super(ApiKey, self).save(*args, **kwargs)
    
        def generate_key(self):
            # Get a random UUID.
            new_uuid = uuid.uuid4()
            # Hmac that beast.
            return hmac.new(str(new_uuid), digestmod=sha1).hexdigest()
    
    
    def create_api_key(sender, **kwargs):
        """
        A signal for hooking up automatic ``ApiKey`` creation.
        """
        if kwargs.get('created') is True:
            ApiKey.objects.create(user=kwargs.get('instance'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my api.py # myapp/api.py from django.contrib.auth.models import User from tastypie.authorization import Authorization
This is my models.py: from tastypie.utils.timezone import now from django.contrib.auth.models import User from django.db
name 'entry_resource' is not defined This is my models.py from tastypie.utils.timezone import now from
from fabric.api import sudo, put, run def install(): run('source /home/user/.virtualenvs/demo/bin/activate') run('pip install requests') if
I'm using django-tastypie and I need to create classes like this from my django
I have used python httplib to implement REST api to connect with Django tastypie.
I'm making an internal API with TastyPie. I have from tastypie.authentication import ApiKeyAuthentication class
I'm trying to go with-the-grain using Django TastyPie to update my models. I have
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement
I am trying to post a new resource to a django tastypie API with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.