[Fixed]-How to manage multiple account(user) types in django

1👍

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.

👤C14L

Leave a comment