Consider an application where there is a company account and employee account. I want to add employees as admin of the company. Then the admin should be able to switch account with the company. Is it possible to do the switch account option? I am a beginner in Django. Can anyone help me with this problem?
I am adding my user model here:
# Custom user
class CustomUser(AbstractUser):
""" Custom user model"""
email = models.EmailField(unique=True, validators=[EmailValidator])
is_company = models.BooleanField(default=True)
is_employee = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
slug = AutoSlugField(populate_from='username')
def __str__(self):
return f"{self.username}"
# Company user
class CompanyAccount(TimeStampedModel):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, related_name='company')
name = models.CharField(max_length=50, null=True, blank=True)
description = models.TextField(null=True, blank=True)
email = models.EmailField(null=True, blank=True)
website_url = models.URLField(null=True, blank=True)
industry = models.CharField(max_length=50, null=True, blank=True)
founded_year = models.PositiveSmallIntegerField(blank=True, null=True)
headquarters = models.CharField(max_length=100, null=True, blank=True)
def __str__(self):
return f"{self.name}"
# Employee user
class Employee(TimeStampedModel):
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, related_name="profile")
first_name = models.CharField(max_length=50, null=True, blank=True)
last_name = models.CharField(max_length=50, null=True, blank=True)
city = models.CharField(max_length=100, null=True, blank=True)
country = models.CharField(max_length=100, null=True, blank=True)
job_title = models.CharField(max_length=100, null=True, blank=True)
company = models.ForeignKey(CompanyAccount, on_delete=models.CASCADE, related_name="employee")
def __str__(self):
return f"{self.user.username}"