Does Tastypie have a helper function to generate API keys?

I figured it out.

First, you make an attempt to get the the user's API key. If it exists, then there will be no error thrown. To regenerate, set the value of the retrieved user's key to None, and then save the key.

If there was an error thrown, then simply create a new key.

try:
    api_key = ApiKey.objects.get(user=someuser)
    api_key.key = None
    api_key.save()

except ApiKey.DoesNotExist:
    api_key = ApiKey.objects.create(user=someuser)

Or, you can just use tastypie's built-in command:

python manage.py backfill_api_keys

Yes, the code for generating the key is defined as an instance method ApiKey.generate_key() which you can use directly.

Here's a simpler version that takes out some of the guesswork of whether the user already exists or not and uses ApiKey.generate_key() directly, rather than implicitly through ApiKey.save(), which I believe makes it a bit more clearer of what's trying to be accomplished:

api_key = ApiKey.objects.get_or_create(user=someuser)
api_key.key = api_key.generate_key()
api_key.save()

UPDATE:

Thus, the shortest version is:

return ApiKey.objects.get_or_create(user=someuser)[0].key

This will generate a new key or return an existing one.