Use self.sdtout and CommandError to print output

This commit is contained in:
Andrea Grandi 2017-06-03 11:06:14 +01:00
parent cf196a4424
commit 34c38e0cfe

View File

@ -1,5 +1,5 @@
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.core.management.base import BaseCommand, CommandError
from rest_framework.authtoken.models import Token
@ -23,7 +23,9 @@ class Command(BaseCommand):
try:
token = self.create_user_token(username)
except UserModel.DoesNotExist:
print('Cannot create the Token: user {0} does not exist'.format(
username
))
print('Generated token {0} for user {1}'.format(token.key, username))
raise CommandError(
'Cannot create the Token: user {0} does not exist'.format(
username)
)
self.stdout.write(
'Generated token {0} for user {1}'.format(token.key, username))