How can I create two user type in Django?

I'm creating a project, where I've two types of account. One is the default User (that use the normal params), and another one that is the account for businesses. Actually the model seem to be the hardest part.

Also, in the future, I would like to add some fields that the normal User model doesn't have (like credit card, sector,...), I need a good way to create the account using the normal User fields that could be updated.

forms.py

class Business_RegisterForm(forms.ModelForm):
    email = forms.CharField(max_length=30)
    password = forms.CharField(max_length=30)

    def clean(self):
        cleaned_data = super(Business_RegisterForm, self).clean()
        
        email = cleaned_data.get('email')
        password = cleaned_data.get('password')
    
    class Meta:
        model = BusinessModel
        fields = [
            'email',
            'password',
        ]

views.py

def Business_RegistrationView(request):
    if request.method == 'POST':
        form = Business_RegisterForm(request.POST)
        if form.is_valid():
        \# clean data
            email = form.cleaned_data\['email'\]
            password = form.cleaned_data\['password'\]

            # user creation
            BusinessModel.objects.create_business(
                email=email,
                password=password
            )
    
            # user auth **credentials
            business = authenticate(
                email=email,
                password=password
            )
            
            # save the model
            business.save()
            form.save()
    
            # login after saved 
            login(request, business)
            
            # redirect to home
            return HttpResponseRedirect('/')  
    else:
        # Request's Error
        print("The POST request has failed.")
        form = Business_RegisterForm()

urls.py

path('business-registration/', Business_RegistrationView, name='business-registration'),

models.py

class BusinessModel(models.Model):
    email = models.OneToOneField(User, on_delete=models.CASCADE, related_name='business_email')
    password = models.OneToOneField(User, on_delete=models.CASCADE, related_name='business_password')

    # BELOW Changes in Admin panel
    class Meta:
        verbose_name = "Business Account"
        verbose_name_plural = "Businesses Accounts"
    
    def __str__(self):
        return self.email

I've tried many times but all of'em didn't worked. I created a model that render the fields from the User model but doesn't work at all, and therefore I've made a model from scratch but doesn't seem the right way to proceed.

Is there any way to create the account Business without starting from scratch, and using the same fields of the User model?

You can inherit from AbstractUser: https://docs.djangoproject.com/en/4.1/topics/auth/customizing/#substituting-a-custom-user-model

Inside the new User model, you can then keep the type of user.

Something like this:

# models.py
class User(AbstractUser):
    TYPE_BUSINESS = 'BUSINESS'
    TYPE_REGULAR_USER = 'USER'
    USER_TYPES = ((TYPE_BUSINESS, 'Business'), (TYPE_REGULAR_USER, 'User'))

    user_type = models.CharField(max_length=10, choices=USER_TYPES)
# views.py
def Business_RegistrationView(request):
    if request.method == 'POST':
        # create your user
        # then assign them the correct type
        user.user_type = User.TYPE_BUSINESS
        user.save()

You can then check if the user is of the correct type wherever you like:

if user.user_type == User.TYPE_BUSINESS:
    # user is a business
Back to Top