0

I want to create a custom User by inherit the AbstractUser:

https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model

But, there is a issue for me, when I use the permissions, there is a IsAdminUser permission.

If I have two custom User models, such as User model, and AminUser model (all of them inherit form AbstractUser). How can I distinguish a user is AminUser or normal User in the custom User model?

user7693832
  • 6,119
  • 19
  • 63
  • 114
  • There is the built-in function `isinstance(obj, class)`. – Klaus D. Nov 06 '17 at 04:45
  • you can simply check the user permission by user permission you can differentiate the users. https://stackoverflow.com/questions/16573174/how-to-get-user-permissions – Kalariya_M Nov 06 '17 at 04:49

1 Answers1

0

In your User Model define a Boolean field as isAdminUser. Now set this Boolean field to false for a normal user and True if the user is an admin. Depending upon the value of isAdminUser you can perform User Specific actions or Admin specific actions.For example:

class ClientUser(AbstractBaseUser):

   username = models.CharField(max_length=20,unique=True)
   name = models.CharField(max_length=20)
   email = models.EmailField(max_length=254,null=True,blank=True)
   company = models.ManyToManyField(ClientCompany,null=True,blank=True)
   date_joined = models.DateTimeField(('date joined'), default=timezone.now)
   is_active   = models.BooleanField(default=True)
   is_admin    = models.BooleanField(default=False)
   is_staff    = models.BooleanField(default=False)
AR7
  • 366
  • 1
  • 16