Reputation: 2678
I have to manage Multiple user types in my application,
I have created User model which has common fields for both these models.
class User(AbstractBaseUser):
#common fields
class PersonType(models.Model):
# persone_type which is a choice field
class Person(User):
# many to many with PersonType
class CompanyType(models.Model):
# company_type which is a choice field
class Company(User):
# many to many with CompanyType
Is this way correct?
How do i manage sign up and login for both types of users.
i.e. While signing up should i create object of User or object of Person/Company depending on what "account_type" field is sent in api data.
If its later option, how do I manage authenticate and login for two models?
Upvotes: 0
Views: 263
Reputation: 12558
I like to keep things simple, if possible.
user_type_choices = (('p', 'person'), ('c', 'company'))
class MyUser(AbstractBaseUser):
...
user_type = models.CharField(max_length=1, choices=user_type_choices)
user_group = models.ManyToManyField(UserGroup)
That leaves room for more user_types later. And then use Django's groups or your own table with "group" definitions.
class UserGroup(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField()
allowed_user_type = models.CharField(max_length=1, choices=user_type_choices)
To ensure that nobody is added to a group they are not allowed in, simply override save()
and check that the user_type
is allowed by the group.
Upvotes: 1