Surya Adhikari
Surya Adhikari

Reputation: 33

django model design with different user types

I am just learning Django so I thought of creating a project called job board to understand more in detail. I have drawn the following use case.

How would be the model design for such a use case? I am confused with the multiple user types in Django. Some favors creating a user profile, while some favors using Groups.

For now, I could only do the following

class User(AbstractUser):
    '''
      Abstract user because django recommends to start with custom user
    '''
    username = None
    email = models.EmailField(_("Email Address"), unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email


class Company(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)

I could create a model for User and Company. But I have no idea on handling multiple user types like user can be either job seeker or recruiter. Also, multiple representatives from a company should be able to register and post jobs as well as there can be independent recruiter as well. How would you handle such a case if you have to? Can anyone help me in a step by step guide, please? This way it will clear my confusion and will help me in better design of tables in the future.

Update with example in a nutshell

class User(models.Model):
    '''
        User can be of any 3 types or can have multiple role as well
    '''
    is_job_seeker = models.BooleanField(default=False)
    is_recruiter = models.BooleanField(default=False)
    is_mentor = models.BooleanField(default=False)

class Company(models.Model):
    user = models.ForeignKey(User) # only user with is_recruiter flag active can be

class JobSeeker(models.Model):
    user = models.OneToOneField(User)
    # job seeker profile related fields like experiences, skills, education, profile image etc

class Recruiter(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    company = models.ForeignKey(Company, null=True, blank=True)
    # recruiter related profile 

Upvotes: 2

Views: 513

Answers (1)

ahmedaljawahiry
ahmedaljawahiry

Reputation: 136

Your implementation is almost there. It doesn't look like you need a custom user model right now, so I would just use Django's default.

I would have something like:

from django.conf import settings
from django.db import models

class Company(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    # Other company-related fields

class JobSeeker(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    # Other jobseeker-related fields

class Recruiter(models.Model):
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
    )
    company = models.ForeignKey(Company, null=True, blank=True)
    # Other recruiter-related fields

When you create any of the models above, you can assign them a user account; and for recruiter, you can assign the company they work for. For example, a company named stack_overflow can have its own company account with a username/password/etc. Then, recruiters who work for stack_overflow could also have their own accounts with their own username/password/etc. Running a command like stackoverflow.recruiter_set will give you all recruiters who work for stack_overflow.

Note that I do not reference User directly. Using the above approach makes your life easier if you decide to switch User models in the future.

I am assuming you don't want to create a User, then create a Company and link it to that user - you just want to do it in one go. That's a slightly different question and the solution will involve you creating a sign-up Form, or something of that sort, where you can add some logic about whether the user is a company, recruiter or jobseeker.

Regarding your other points, it looks like you're looking to set user permissions. Here are the docs for setting default permissions for your custom users, and here are the general docs for Django's built-in permissions system. For example, your Company and Recruiter model could return True for has_perm('your_app.add_job'), while your Jobseeker model returns False. I.e. Companies and Recruiters can create Jobs, but jobseekers cant.

Hope this helps!

Upvotes: 1

Related Questions