Doge
Doge

Reputation: 113

How to have a Custom User model for my app while keeping the admins working as default in Django?

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?

Upvotes: 3

Views: 136

Answers (2)

dmitryro
dmitryro

Reputation: 3506

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'

Upvotes: 2

Doge
Doge

Reputation: 113

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.

Upvotes: 0

Related Questions