3

Here is what I am trying to accomplish:

- Have admins login to the admin page using the default way (username and password).

- Have users register/login to my web app using a custom User Model which uses email instead of password. They can also have other data associated that I don't need for my admins.

- Separate the admin accounts and user accounts into different tables.

I checked how to create a Custom User class by extending AbstracBaseUser, but the result I got is that my admins also became the new user type. So can I have the Custom User model be used for my app users while keeping the default admin system untouched? Or what is a good alternative to my design?

Doge
  • 103
  • 6
  • Do you have a very compelling reason to do this? – e4c5 Oct 04 '16 at 02:16
  • @e4c5 I just felt that the two are different things and should be not mixed-up. But I guess I don't have a really compelling reason besides my personal bias. I guess I will go with the standard design. – Doge Oct 04 '16 at 02:24
  • The standard design is there for a good reason. if two objects are similar they should be the same model. You just need to have flag or 'type of' column to tell them apart. And the user model already has that. Two of them in fact. – e4c5 Oct 04 '16 at 03:35

2 Answers2

2

The recommended Django practice is to create a OneToOne field pointing to the User, rather than extending the User object - this way you build on top of Django's User by decorating only the needed new model properties (for example):

class Profile(models.Model):
    user = models.OneToOneField(User,parent_link=True,blank=True,null=True)
    profile_image_path =  models.CharField(max_length=250,blank=True, null=True)
    phone = models.CharField(max_length=250,blank=True, null=True)
    address = models.ForeignKey(Address,blank=True,null=True)
    is_admin = models.NullBooleanField(default=False,blank=True,null=True)

    class Meta:
       verbose_name = 'Profile'
       verbose_name_plural = 'Profiles'
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • It's just an example - could be is_guest, is_external - any other. – dmitryro Oct 04 '16 at 03:37
  • Different implementations use different meaning - admin role might be different for different use cases, guest, if defined, might have certain privelegies different from anonymous. Django permissions allow flexibility with custom roles. I might need to create a semi-admin semi-staff with custom Django permissions. – dmitryro Oct 04 '16 at 03:42
0

After couple more hours of digging, I think it is best to keep a single User model and use permissions and roles for regulations.

There are ways that can make multiple different user model authentications work, such as describe in here: How to have 2 different admin sites in a Django project? But I decided it wasn't worth it for my purposes.

Community
  • 1
  • 1
Doge
  • 103
  • 6