How to programmably create an User in Django?

Question:

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?


Here are my models:

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'
Asked By: Kazzz Studio

||

Answers:

You can create user from command line using below method:

user@hostname$ python3 -m django shell
>>> import django.contrib.auth
>>> User = django.contrib.auth.get_user_model()
>>> user = User.objects.create_user(username='username', password='userpassword')

Observations:

  1. A User created, is available for the whole project and not just a single app
  2. By default, is_superuser and is_staff are both False when a User is created unless they are overwritten by passing in create_user() method.
Answered By: 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()