Need to create multi level users with roles having same validation for the models as that specified in admin.py, only that each of these users will have different roles.eg. Customer can have different custom_users with diff. roles. A customer can create multiple such custom users with different roles. Each customer can see only his own records in the admin panel. Models: Customer, Custom_user(customer f.key), Role(permission) , customer_customuser(custom_user_id,role_id), But I need to use the same permissions as available in the original django default admin panel for the diff.models like add,edit,delete, view. But have also customized it in admin.py such that each customer can administer using changelist_view and filtering the queryset objects accordingly. How can this be achieved. The models:
class Role(models.Model):
name = models.CharField(max_length=100, unique=True)
permissions = models.ManyToManyField(Permission)
def __str__(self):
return self.name
class CustomUserManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not email:
raise ValueError("The Email field must be set")
email = self.normalize_email(email)
user = self.model(username=username, email=email)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password=None):
user = self.create_user(username, email, password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(max_length=150, unique=True)
email = models.EmailField(unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
# Specify a related_name below
role = models.ForeignKey(Role, on_delete=models.CASCADE,
related_name='custom_users')
# # Specify a related_name for the groups below
groups = models.ManyToManyField(Group, blank=True,
related_name='custom_users')
# Add a custom related_name for user_permissions below
user_permissions = models.ManyToManyField(
Permission,
blank=True,
related_name='custom_users_permissions'
)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
def __str__(self):
return self.username
# class Meta:
# permissions = [
# ("can_view_custom_user", "Can view custom users"),
# # Define other permissions here as needed
# ("view_domain", "Can view domain"),
# ("add_domain", "Can add domain"),
# ("change_domain", "Can change domain")
# ]
# Create your models here.
class AbstractPerson(models.Model):
first_name = models.CharField(max_length=60)
last_name = models.CharField(max_length=60)
email = models.EmailField(unique=True)
mobile = models.CharField(max_length=10, validators=[mobile_regex])
muser_id = models.IntegerField(null=True, blank=True)
class Customer(AbstractPerson):
#multiadmin line
# Each Customer can have multiple CustomUser instances with different roles
custom_users = models.ManyToManyField(CustomUser,
through='CustomerCustomUser')
Roles should be the same as available in the default django users/groups
from multilevel user admin in django
No comments:
Post a Comment