Create user from the command line in Django

14,249

Solution 1

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()

Solution 2

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()
Share:
14,249
Kazzz Studio
Author by

Kazzz Studio

I am working at Kazzz Studio as a founder and software developer. My jobs are designing business planing, making profit model, designing software and so on.

Updated on June 27, 2022

Comments

  • Kazzz Studio
    Kazzz Studio almost 2 years

    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'