1

I want to run my custom command from django view which create new user. Here is my command

python manage.py  tenant_command createsuperuser --schema=schema_name

Here schema name my be change

Above command is same as

python manage.py createsuperuser

Here i didn't know how to pass username, email and password and confirm password any suggestion would be appreciated

Ytech Python
  • 73
  • 10
  • 1
    Does this answer your question? [Run custom admin command from view](https://stackoverflow.com/questions/6250970/run-custom-admin-command-from-view) – JPG Feb 27 '20 at 05:55
  • up-to some level but i also need to pass username, email, and password, here I'm using custom user model extends from abstract base user – Ytech Python Feb 27 '20 at 06:03
  • check this - https://stackoverflow.com/questions/6244382/how-to-automate-createsuperuser-on-django – anuragal Feb 27 '20 at 06:12
  • Why use a management command? Just create the User using the [User API](https://docs.djangoproject.com/en/3.0/topics/auth/default/#creating-users) – dirkgroten Feb 27 '20 at 10:50
  • Yeah you are right, that was my mistake – Ytech Python Feb 27 '20 at 11:02

1 Answers1

2

There are two ways to achieve what you trying to do:

  • Using code to change the user object and save it to database
  • Calling the command you mentioned

According to django docs you can call your commands in code using call_command function, it takes arguments and pass them to command too:

call_command("custom_command", arguments..)

In the other way you have access to User model in your view so you can import it directly and create the user:

from django.contrib.auth impor get_user_model

get_user_model().objects.create()
Glyphack
  • 876
  • 9
  • 20
  • Thanks for you helps, Here I'm using different schema for different tenants, somethings like isolated schema, Anyway I've solved my issue – Ytech Python Feb 27 '20 at 10:58