Autocreate superuser with each schema django
Asked Answered
A

1

1

I'm working on a multi tenant app using django-tenant-schema(and postgresql). Now as soon as the new schema is getting created, I'm using the call_command to create a superuser. I also thought of the option of using signal post_migrate.

My doubt is how can I auto create a superuser without typing the password in shell. The use case is as soon as the user registers for his/her schema, a superuser is generated and the user logs in with the superuser username and password and can change the password thereafter.

I'm using the following to call the createsuperuser command:

call_command('createsuperuser', schema_name=self.schema_name, username='admin', email='[email protected]')

One way to solve the issue is to change Django's createsuperuser.py file and provide a password over there automatically, buts thats not an acceptable route, ie cahnging the source code for some use case. Could I please request for some better methods

Antilepton answered 24/6, 2016 at 18:2 Comment(0)
T
0

you can "prepare" password for new superuser and pass md5 value to bash:

a=# select md5('some password'||'test');;
               md5
----------------------------------
 d5b3eb515b64edf295b2ee9062946d24
(1 row)

a=# create user test superuser password 'md5d5b3eb515b64edf295b2ee9062946d24';
CREATE ROLE

so you can use d5b3eb515b64edf295b2ee9062946d24 in bash, saving password somewhere else?..

Torruella answered 24/6, 2016 at 18:38 Comment(7)
U c, my issue is not with security as the user will have to change the password before proceeding. But my issue is how can I pass the value, not through bash, but automatically through code to django. Even a superuser without password is ok, so that for first time use the user will have to give a passwordAntilepton
Sorry if I'm not clear. My question is how to automatically send this password to Django, i.e. as the user registers, the call_command calls the createsuperuser, but this is asking for password in the command line. I want the password to be automatically sent, ie not through command line but through some programming/codesAntilepton
sorry - I'm not friendly with django :) why don't you generate password yourself? then you know it and nothing have to send it anywhere - you have it in code before you create user...Torruella
I can solve the issue by changing the django createsuperuser.py file my storing string in the password field. But that is not an acceptable solution. I should not change the source code of django for certain use case and then get screwed up for some other use case that could come in future.Antilepton
Well, u c, I'm planning an inventory app, so a user registers his/her firm and he'she is the superuser. They can only create further sub users (Staffs, etc) for their firm in the specific schema. I cannot create passwords for users manually, there has to be a way for this to be done using codes!!Antilepton
ah. I c. well you could plpgsql it returning generated password to ui. but if it is some Django management command - I can't be helpful... Leaving this answer for comments to be accessible to others.Torruella
Thanks Vao for helping me out, with your best!!! Maybe in the worst case I'll do the dreadful - break Django ORM and plpgsql it!!Antilepton

© 2022 - 2024 — McMap. All rights reserved.