I want to create multiple types of user in django app e.g one for Company and one for Employee.
What I had in mind was company will signup for itself and then employees will be created by company admin through his/her dashboard.
After creation employees will sign in directly.
So (if possible) only one sign in form can be used for company and employees.
class Company(models.Model):
name = models.CharField(max_length=150)
logo = models.CharField(max_length=1000, blank=True)
admin_name = models.CharField(max_length=200)
admin_email = models.CharField(max_length=200) # can be used as 'username'
website = models.CharField(max_length=200, blank=True)
class CustomUserManager(auth_models.BaseUserManager):
def create_user(self, email, first_name, emp_id, password):
#
def create_superuser(self, email, first_name, emp_id, password):
#
class Users(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
company = models.ForeignKey(Company, on_delete=models.CASCADE, null=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
profile_pic = models.CharField(max_length=2000, blank=True)
email = models.EmailField(unique=True, null=True)
emp_id = models.CharField(max_length=50)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'emp_id', ]
objects = CustomUserManager()
I searched almost everywhere but couldn't find anything. If there is any way to solve this issue please let me know otherwise if you know any trick to get this thing done then it is also welcome. Thanks in advance.
You can just create groups for the different types of users:
Group.objects.create(name='Company')
And then assign the user to the appropriate group:
user.groups.add(group)
The last thing it sounds like you could do is to associate employees with the company user, so you can add a self reference to your Users model like this:
company_user = models.ForeignKey("self", null=True, blank=True)
So, for those employees, you can then associate the user with that company user and leave it blank for the company user itself.
Although, you don't need to do that if you want to use the company foreign key you already have to track the related users that way. But if a company may have several "company users", you could then group up the employees to each.
I hope that helps.