12

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'
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Kazzz Studio
  • 449
  • 1
  • 6
  • 19
  • do you have custom user models for each app? I don't get the issue? – almost a beginner Sep 02 '17 at 03:49
  • when you create an user, is in the entirely project not just in the app – Mauricio Cortazar Sep 02 '17 at 04:05
  • I don't use custom user model. I use one-to-one model for extends User. Do I have to use custom user model? – Kazzz Studio Sep 02 '17 at 05:11
  • django users belong to all your apps, not just one. enduser and admin user is defined by you. when you create a superuser, it will be able to administer all the apps, not just one. Give us an example of what App1, App2 and App3 could be? – almost a beginner Sep 02 '17 at 05:23
  • 1
    I added models code. This app is something like online consultation system. Enduser can take consultation service using smartphone. Doctor can give a service through PC web browser. And I have another kind of user as system administrator. They must maintain the system. – Kazzz Studio Sep 02 '17 at 06:20

2 Answers2

37

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.
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • Thank you for reply. In django, can't I make 3 different user authentication function in one project? I don't make sense because many web service must have enduser login function as well as system administrator's manage function. Do I have to separate project? – Kazzz Studio Sep 02 '17 at 05:10
  • But that can be achieved by first creating a normal login and then getting higer level access. So that they can manage from both perspective. – Astik Anand Sep 02 '17 at 05:44
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()
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
  • Alternatively, you can write `from APPLICATION_NAME.models import User` & you can just write `user1 = User(...)`. Replace APPLICATION_NAME with the specific application you want to retrieve models from – waffledood Mar 19 '23 at 13:23