Create Tenant Superuser Command
python manage.py createtenantsuperuser --tenant-id=<tenant_id>
Overview
Creates a Django superuser for a specific tenant with elevated permissions and admin access.
Purpose
Extends Django's built-in createsuperuser command to work within tenant context, allowing: - Creation of admin users per tenant - Superuser privileges within tenant scope - Proper tenant data isolation - Django admin access for tenant
Usage
Basic Usage
python manage.py createtenantsuperuser --tenant-id=acme
With Additional Options
python manage.py createtenantsuperuser \
--tenant-id=acme \
--username=admin \
--email=admin@acme.com \
--noinput
Required Arguments
--tenant-id (Required)
Specifies which tenant to create superuser for:
python manage.py createtenantsuperuser --tenant-id=acme
If tenant doesn't exist:
Error: Tenant with id 'acme' does not exist
Optional Arguments
All Django superuser arguments are supported:
--username- Username for superuser--email- Email address--no-input- Non-interactive mode--preserve- Preserve existing password
Example interactive session:
$ python manage.py createtenantsuperuser --tenant-id=acme
Using tenant: ACME Corporation
Username: admin
Email address: admin@acme.com
Password: ****
Password (again): ****
Superuser created successfully.
Interactive Mode
When run without --noinput, prompts for:
- Username - Unique username for superuser
- Email - Email address
- Password - Secure password (prompted twice for confirmation)
Example:
$ python manage.py createtenantsuperuser --tenant-id=globex
Using tenant: Globex Corporation
Username: admin
Email address: admin@globex.com
Password:
Password (again):
Superuser created successfully.
Non-Interactive Mode
For automation, use --noinput with all required fields:
python manage.py createtenantsuperuser \
--tenant-id=acme \
--username=admin \
--email=admin@acme.com \
--noinput
This creates superuser without prompts (password must be set separately).
What Happens
- Validates tenant exists with specified tenant_id
- Sets tenant context for current operation
- Creates Django User object in tenant's database
- Marks user as staff and superuser
- Sets secure password
- User now has full admin access within tenant
Tenant Context
The superuser is created in the correct tenant context:
- User created in tenant's database/schema
- User can only access tenant's data
- Admin interface isolated to tenant
- Data cannot be accessed by other tenants
Example:
# Create admin for acme
python manage.py createtenantsuperuser --tenant-id=acme --username=admin
# Admin can now access:
# - Django admin at /admin/
# - acme's data only
# - Cannot see globex data
Accessing Django Admin
After creating superuser, admin can access:
https://acme.example.com/admin/
Username: admin
Password: [password set during creation]
Admin sees only: - ACME tenant's data - ACME tenant's users/groups - ACME tenant's configuration
Error Handling
Tenant Not Found
Error: Tenant with id 'unknown' does not exist
Username Already Exists
Error: User with this username already exists.
Invalid Email
Error: Enter a valid email address.
Security Considerations
- Superuser has full admin access within tenant
- Password should be strong and secure
- Consider role-based access control for production
- Audit superuser creation and changes
- Use non-interactive mode carefully in CI/CD
Related Commands
createtenant- Create new tenantmigratetenant- Run migrations for tenantshell- Django shell with tenant contextchangepassword- Change superuser password