pythonBeginner
pythonBeginner

Reputation: 791

proper way of handling multiple user type in django 1.10

I have researched a bit in the google and found out many different solutions for handling multiple user type. To make my question more clear, here is the requirement

1) There is two types of user - (one is general and another is developer)

2) Both user types will have same form field - (username, email, password, password(confirm))

The below is what I have tried.

class customUser(AbstractBaseUser):
    user_choice = (
        ('G', 'General'),
        ('D', 'Developer'),
    )
    user_type = models.charField(choices=user_choices, max_length=2, default='G')

class mainUser(customUser):
    type = models.OneToOneField('customUser')

Do I have to handle explicitly for username, email, password and re-password field?

I am using django allauth for single user registration and login. Do I have to now work it differently?

Upvotes: 1

Views: 202

Answers (1)

Laurent S
Laurent S

Reputation: 4326

If all you need is different logic in views, I'd do what Jens suggests in the comments. Extend the base User model with a Profile, something like this:

from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user_choice = (
        ('G', 'General'),
        ('D', 'Developer'),
    )
    user_type = models.charField(choices=user_choices, max_length=2, default='G')

Then either add a way to select user_type at signup, or just provide 2 different views to signup.

And in your views, where you need logic that depends on account type:

def my_view(request):
    if request.user.profile.user_type == 'G':
         # do stuff for General users
    else:
         # do Developer stuff

Upvotes: 1

Related Questions