You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
970 B

  1. from django.contrib.auth.management.commands import createsuperuser
  2. from django.core.management import CommandError
  3. class Command(createsuperuser.Command):
  4. help = 'Non-interactively create an admin user'
  5. def add_arguments(self, parser):
  6. super(Command, self).add_arguments(parser)
  7. parser.add_argument('--password', default=None,
  8. help='The password for the admin.')
  9. def handle(self, *args, **options):
  10. password = options.get('password')
  11. username = options.get('username')
  12. if password and not username:
  13. raise CommandError('--username is required if specifying --password')
  14. super(Command, self).handle(*args, **options)
  15. if password:
  16. database = options.get('database')
  17. db = self.UserModel._default_manager.db_manager(database)
  18. user = db.get(username=username)
  19. user.set_password(password)
  20. user.save()