Django - Multiple user types (ability to switch between them)

51
December 03, 2019, at 01:10 AM

I am doing some pre-planning on a project I am working on and my approach to it. A part of the app is the ability to have multiple user types. For example, a Company and a Contractor account type. Though it's important to note that a company can also be a contractor.

So the approach I was going to attempt to implement is similar to Upwork where there is one account, but they can switch between being a client and a freelancer if they wish. It would be ideal if the user didn't need to log out while switching between the two. I'm newer to the django framework and was hoping someone could shed light on the best possible approach to accomplish this while also taking into account scale-ability.

Answer 1

If you are going to be using a custom user model, here is one way I would approach the problem (details follow the code):

class User(AbstractBaseUser):
    ...
    USER_TYPE_CHOICES = [
        ('COM', 'Company'),
        ('CON', 'Contractor'),
    ]
    user_type = models.CharField(max_length=3, choices=USER_TYPE_CHOICES)
    ...
    def save(*args, **kwargs):
        # Try to retrieve the current object from the database.
        try:
            db_obj = User.objects.get(pk=self.pk)
        except User.ObjectDoesNotExist:
            db_obj = None
        if db_obj
            # Check whether user_type is not to be updated (not in update_fields,
            # or hasn't changed its value).
            update_fields = kwargs.get('update_fields')
            if (update_fields and 'user_type' not in update_fields) or self.user_type == db_obj.user_type:
                super().save(*args, **kwargs)
                return
        # At this point, either the current object is being newly created,
        # or its user_type field is about change.
        if self.user_type == 'COM':
            ...  # Make changes when the user switches to Company mode
        elif self.user_type == 'CON':
            ...  # Make changes when the user switches to Contractor mode
        super().save(*args, **kwargs)

I add a field with choices set, and I override the save method to let me (re)configure the object's functionality based on whether it is being saved as a Company or a Contractor.

Benefits:

  • Single user model
  • No need for a user to log out
  • Scalable (adding more user modes is only a matter of updating the choice set and perhaps adding some custom saving logic)
READ ALSO
Function to find number of 'ordered combinations' with no particular length of a list Python

Function to find number of 'ordered combinations' with no particular length of a list Python

While variants of this question have been asked numerous times on this site, I haven't found any information on how to do 'ordered combinations' with a given list

63
jQuery submit function add data several times

jQuery submit function add data several times

I have this function I use to store items (voucher codes) based on some rules

50
How to use a select's value to show a certain div(a div of radio buttons)?

How to use a select's value to show a certain div(a div of radio buttons)?

My goal: The user picks selection 1 and the corresponding div will showThe user picks selection 2, the selection 1 div disappears and shows the selection 2's div

73
How can I run jQuery function only if user hasn’t scrolled?

How can I run jQuery function only if user hasn’t scrolled?

I’ve been trying to incorporate an auto scroll function on my website (live here)I want it to scroll to the content 6 seconds after load, but only if the user is on the very top of the page and hasn’t scrolled enough to uncover the #introduction element

64