Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create user from the command line in Django

I am confusing now. I have 3 apps in one project.

App1 : use from enduser(web view based app)

App2 : use from service provider(web service)

App3 : use from system administrator.

I want to use django authentication system for each apps. I can make django project to authenticate App1's user. But, how can I use authentication system of App2 and App3 at the same time.

When I run python manage.py createsuperuser command, django make App1's superuser. How can I make for App2 and App3 using this command?

Does someone have any idea? Please help me.

Models.py

### This table is for end user.
class RemoshinUser(models.Model):

    user = models.OneToOneField(User)
    user_type = models.SmallIntegerField(default=1)
    kana_name = models.CharField(max_length=128, blank=True)
    date_of_birth = models.DateField(blank=True, null=True)
    sex = models.SmallIntegerField(null=True)
    postno = models.CharField(max_length=7, blank=True)
    address1 = models.CharField(max_length=128)
    address2 = models.CharField(max_length=128, blank=True)
    telno = models.CharField(max_length=16, blank=True)
    photo = models.ImageField(upload_to='users/%Y/%m/%d', blank=True)

    create_date = models.DateTimeField(auto_now_add=True)
    modify_date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'remosys_remoshin_user_tbl'
        swappable = 'AUTH_USER_MODEL'


### This table is for service provider.
class RemoshinDoctor(models.Model):
    user = models.OneToOneField(User)
    user_type = models.SmallIntegerField(default=2)
    doctor_id = models.CharField(max_length=16, primary_key=True)
    clinic_id = models.ForeignKey(Clinic)
    photo = models.ImageField(blank=True, null=True)
    create_date = models.DateTimeField(auto_now_add=True)
    modify_date = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'remosys_remoshin_doctor_tbl'



### This table is for system administrator.
class RemoshinManager(models.Model):
    user = models.OneToOneField(User)
    user_type = models.SmallIntegerField(default=3)
    manager_id = models.CharField(max_length=16, primary_key=True)
    create_date = models.DateTimeField(default=timezone.now)
    modify_date = models.DateTimeField(default=timezone.now)

    class Meta:
        db_table = 'remosys_remoshin_manager_tbl'
like image 608
Kazzz Studio Avatar asked Sep 02 '17 03:09

Kazzz Studio


People also ask

How use Django command line?

Django can be installed easily using pip . In the command prompt, execute the following command: pip install django . This will download and install Django. After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.

What is the Django command to create a user or password for the admin user interface?

To create a new admin user in Django, we use the createsuperuser command, and this command can be executed using the manage.py utility. Once we execute the given command, it will prompt some fields like Username, Email, Password, Confirm Password.


2 Answers

You can create user from command line using below method. But whenever user is created, it is created for whole project and not for the one app.

user@hostname$ python3 -m django shell
>>> import django.contrib.auth
>>> User = django.contrib.auth.get_user_model()
>>> user = User.objects.create_user('username', password='userpassword')
>>> user.is_superuser = False
>>> user.is_staff = False
>>> user.save()
like image 121
Astik Anand Avatar answered Oct 06 '22 22:10

Astik Anand


If you haver extended the base user model then change the first command in @Astik Anand answer to:

from django.contrib.auth import get_user_model

User = get_user_model()
like image 34
Christopher Broderick Avatar answered Oct 06 '22 22:10

Christopher Broderick