I'm making a django website and I had made groups for the admin page access(Admin, Content Writer, Users). What I want is when a user signs up they should be assigned the specific group based on from which form they are signing in/up.
Here is my models.py file for accounts app
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
# Custom User Manager
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, first_name, last_name=None, **extra_fields):
if (not email):
raise ValueError("Email Must Be Provided")
if (not password):
raise ValueError("Password is not Provided")
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, first_name, last_name=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_active', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, first_name, last_name, **extra_fields)
def create_user_with_groups(self, email, password, first_name, last_name=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, first_name, last_name, **extra_fields)
def create_superuser(self, email, password, first_name, last_name=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_active', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, first_name, last_name, **extra_fields)
# Custom user Model
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=254)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255, null=True, blank=True)
mobile = models.CharField(max_length=50, null=True, blank=True)
address = models.CharField(max_length=250, null=True, blank=True)
# profile_pic = models.ImageField(null=True, blank=True)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name']
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
here is what my Groups look like in admin page
In my CustomUserManager class, can we add one more method, like create_content_writer, which has the functionality of create_user but it sets the is_staff to true and is_active to true and adds that user to Content Writer group when it is created.
Or if there is more efficient way of doing so, then please tell, because in future if the groups are gonna increase then I'll have to make separate function for each group. If all of this can be achieved from one single function, it would be really great!
