from django.core.management.base import BaseCommand from django_auth_ldap.backend import LDAPBackend class Command(BaseCommand): help = "Import a user from LDAP by username" def add_arguments(self, parser): parser.add_argument("username", type=str) def handle(self, *args, **options): username = options["username"] user = LDAPBackend().populate_user(username) if user is None: raise Exception(f"No user named {username}") else: print("Imported user:", user)