2

I'm building a multi-tenant project with django-tenant.

The issue I'm encountering is that "password" is not a valid flag option.

    management.call_command(
        'create_tenant_superuser', 
        interactive = False,
        username = "user"
        password = "password"
        ) 

gives error:

TypeError: Unknown option(s) for create_tenant_superuser command: password Valid options are: ...
illevens
  • 343
  • 1
  • 13
  • Why do you want to use `call_command` when you can directly create the user? Use the [`create_superuser` (Django docs)](https://docs.djangoproject.com/en/3.2/ref/contrib/auth/#django.contrib.auth.models.UserManager.create_superuser) method of the user model's manaager. – Abdul Aziz Barkat Apr 27 '21 at 17:12
  • @AbdulAzizBarkat because I am using a django-tenants package that wraps create_superuser in it's own code in order to only create superuser for particular tenant / in particular postgres schema. – illevens Apr 27 '21 at 18:34
  • I am already directly creating a superuser within a tenant app, but right now I'm trying to create an api which registers tenants and their superusers; package only provides a CLI command for that : create_tenant_superuser – illevens Apr 27 '21 at 18:35
  • 1
    Please [edit] your question and clarify this there. I have posted an answer that will solve this problem. – Abdul Aziz Barkat Apr 27 '21 at 19:27

1 Answers1

1

You can use the schema_context [django-tenants docs] context manager to make queries to specific schemas by providing the schema name:

from django.contrib.auth import get_user_model
from django_tenants.utils import schema_context


UserModel = get_user_model()

with schema_context(schema_name):
    UserModel.objects.create_superuser(username="user", password="password")
Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33