[Fixed]-Best way to handle multiple type of users in Django

1👍

The first solution is much better.

The downside here is that there unused fields.

I disagree with this, you don’t have to store all the fields within the User model. Also, if you’re talking about 5 fields for example, that doesn’t really matter.


You can extend AbtractUser and also use some composition; you don’t have to put all the fields there:

class User(AbstractUser):
     email = ...
     ...
     # for the employer, let's say you want to save the company details
     company = models.ForeignKey('myapp.Company', null=True, blank=True)
     ...
     # for the customer
     customer_details = models.ForeignKey('...')

This way you could record a user_type if you want or deduce the type from the foreign key (if there is a company, it’s an employer).


To help you more with the model, I need to know what differentiate an employer from a customer in your application. Note that with that solution, an instance of User could be both.


Concerning the permissions, I feel like it’s a separate problem; I’d recommend you to sort it last. If the design you pick is close to the reality and you get the features working; adding custom permissions will be really easy.

Leave a comment