I’m facing issues with managing different types of users. And need to customize the admin panel for each users having custom permission. This is my first major project and do not have any mentor to guide me.
Following are the models, I’m working with. The actual models have more fields.
class User(AbstractUser):
'''
Regional Admin and Admin can login to the admin panel.
Regional Admin can only access the nodes data of regions they are assigned to.
'''
class UserTypeChoices(models.TextChoices):
ADMIN = 'ADMIN', 'Admin'
REGIONAL_ADMIN = 'REGIONAL_ADMIN', 'Regional Admin'
NORMAL_USER = 'NORMAL_USER', 'Noramal User'
email = models.EmailField(unique=True, validators=[EmailValidator(message="Invalid Email Address")])
user_type = models.CharField(max_length=20, choices=UserTypeChoices.choices, default=UserTypeChoices.NORMAL_USER)
def __str__(self) -> str:
return self.username
# Country Model
class Country(models.Model):
name = models.CharField(max_length=100, unique=True)
population = models.PositiveBigIntegerField(validators=[MinValueValidator(0)], default=0)
class Meta:
verbose_name_plural = "Countries"
def __str__(self) -> str:
return self.name
# Each country might be divided into one or more zones
class Region(models.Model):
country = models.ForeignKey(Country, on_delete=models.CASCADE)
name = models.CharField(max_length=100) # East West North South
def __str__(self) -> str:
return self.name
# Each nodes represents some point in a particular region
class Node(models.Model):
region = models.ForeignKey(Region, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
length = models.FloatField(validators=[MinValueValidator(0)])
def __str__(self) -> str:
return self.name
The Admin here is the super user and regional admin is a staff user and can login to the admin panel. Each regional user can access region wise data of the Node.
I want to relate the Regions with Users.
Let there are four regions in the region table: East, West, North and South and Each node is related to one of the regions.
If the regional user has the access to a particular region he can perform CRUD operations on the Nodes in that particular regions. One Regional Admin can have access to the multiple regions.
For example if the regional user has the permission to access nodes in East and North regions, he can perform CRUD operations on nodes in East and North region.
I was learning about creating Custom Permission for each types of users and I can create views and check permission easily.
But I want these editing options in the Admin panel.Preformatted text