[PYTHON] Manage multiple types of users with CustomUser in django-allauth (multi type user)

Overview

――I want a system that can create multiple user types with Django --Allows you to create two types of users, "Supply user" and "Buyer user" --Give different attributes to each user type --Allow supply users to register company names and buyer users to register nearest stations --The environment is - Python 3.6.9 - Django 3.1.1 --django-allauth 0.42.0 (social login function)

Python + Django restrictions

--Only one model class can be used for user authentication (login and sign-up) --Specified by ʻAUTH_USER_MODEL` in settings.py

Implementation overview

--Create one CustomUser model and have userType --Information for each user type is stored in a separate table and linked with the custom user class with ʻOneToOneField. - UserDetailSupplier - UserDetailBuyer --Use an adapter to save the user. --Create AccountAdapter by inheriting ʻallauth.account.adapter.DefaultAccountAdapter --Implement the save_user method and separate the save process for each userType in it. --Specify the above class with ʻACCOUNT_ADAPTERin settings.py --The sign-up template is divided by type ~~, but the destination is one ~~ and the destination is also divided into two. --Createsignup and signup_supplier` --One template for login

Relationship between django-allauth and your own class

--django-allauth uses the app name ʻaccount, but separately create an app called member and manage it there. --When you google, there are many examples of making it with the plural form ʻaccounts, but it is difficult to distinguish it from the allauth side and I did not want to make it plural, so I gave it a different name. --Similar to Python member functions, but the idea that it wouldn't be confused because it's an app name

problem

-~~ If an error occurs due to duplicate accounts when signing up, the original template will be restored instead of the added template ~~ --~~ If you try to sign up with signup_supplier and fail, it will transition to signup ~~ --Solve the form submission destination in two parts. --I'm using request.POST to get the added item from the form, but what can I do? --I tried to get it from form.cleaned_data.get, but I couldn't get it because it was empty. --Since the user information table is separated, it is difficult to understand the admin screen. It seems better to make your own management screen

Specific example of implementation

Install django-allauth

--Install with pip and make various settings

Configuration

--Add the following to the config file


# settings.py

#Specify the model to use for authentication
AUTH_USER_MODEL = 'member.CustomUser'
#Specify the adapter to save the information from signupform in customusermodel
ACCOUNT_ADAPTER = 'member.adapter.AccountAdapter'

Creating a model class


# member/models.py

from django.contrib.auth.models import AbstractUser
from django.db import models
from django.contrib.auth.models import PermissionsMixin, UserManager

class UserType(models.Model):
    """User type"""
    typename = models.CharField(verbose_name='User type',
                                max_length=150)

    def __str__(self):
        return f'{self.id} - {self.typename}'

USERTYPE_SUPPLIER = 100
USERTYPE_BUYER = 200
USERTYPE_DEFAULT = USERTYPE_BUYER

class CustomUserManager(UserManager):
    """Manager for extended user model"""

    def _create_user(self, email, password, **extra_fields):
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')
        return self._create_user(email, password, **extra_fields)


class CustomUser(AbstractUser):
    """Extended user model"""

    class Meta(object):
        db_table = 'custom_user'

    #Use the created manager class
    objects = CustomUserManager()

    #Have a user type in the model
    userType = models.ForeignKey(UserType,
                                verbose_name='User type',
                                null=True,
                                blank=True,
                                on_delete=models.PROTECT)
    def __str__(self):
        return self.username

class UserDetailSupplier(models.Model):
    user = models.OneToOneField(CustomUser,
                                unique=True,
                                db_index=True,
                                related_name='detail_supplier',
                                on_delete=models.CASCADE)
    #Items for supplier users
    companyName = models.CharField(
                                   max_length=100,
                                   null=True,
                                   blank=True,
                                )
    def __str__(self):
        user = CustomUser.objects.get(pk=self.user_id)
        return f'{user.id} - {user.username} - {user.email} - {self.id} - {self.companyName}'

class UserDetailBuyer(models.Model):
    user = models.OneToOneField(CustomUser,
                                unique=True,
                                db_index=True,
                                related_name='detail_buyer',
                                on_delete=models.CASCADE)
    #Items for buyer users
    nearestStation = models.CharField(
                                   max_length=100,
                                   null=True,
                                   blank=True,
                                )
    def __str__(self):
        user = CustomUser.objects.get(pk=self.user_id)
        return f'{user.id} - {user.username} - {user.email} - {self.id} - {self.nearestStation}'

Creating an adapter


# member/adapter.py

from allauth.account.adapter import DefaultAccountAdapter
from .models import *

class AccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=True):
        """
        This is called when saving user via allauth registration.
        We override this to set additional data on user object.
        """
        # Do not persist the user yet so we pass commit=False
        # (last argument)
        user = super(AccountAdapter, self).save_user(request, user, form, commit=False)
        #user.userType = form.cleaned_data.get('userType')
        user.userType = UserType(request.POST['userType'])

        if not user.userType:
            user.userType = UserType(USERTYPE_DEFAULT) #Set default user type

        #Save once to get the user ID
        user.save()

        if int(user.userType.id) == USERTYPE_SUPPLIER:
            #Supplier user
            supplier = UserDetailSupplier()
            supplier.user_id = user.id
            supplier.companyName = request.POST['companyName']
            supplier.save()
        else:
            #Other than that, general users
            user.userType = UserType(USERTYPE_BUYER)
            buyer = UserDetailBuyer()
            buyer.user_id = user.id
            buyer.nearestStation = request.POST.get('nearestStation', False)
            buyer.save()

Creating a template

--Overwrite django-allauth template - templates/account/signup.html - templates/account/signup_supplier.html --Enter the user type with hidden in the form. <input type =" hidden "name =" userType "value =" 1 "/> --Both POST destinations are {% url'account_signup'%}

urls.py settings

--Added the following


    path('member/signup_supplier/', TemplateView.as_view(template_name = 'account/signup_supplier.html'), name='signup_supplier'),

Other

--Add each model class to admin.py

reference

-Create an authentication feature with django-allauth and CustomUser in Django

--Search for "django-allauth userType" on google https://www.google.com/search?q=django-allauth+userType&oq=django-allauth+userType&aqs=chrome..69i57.9683j0j1&sourceid=chrome&ie=UTF-8

-[Python / Django] What to do if you want to use AbstractUser twice

-Automatically input arbitrary initial data when migrating with Django

-Django textbook << practice >> that can be used in the field

Recommended Posts

Manage multiple types of users with CustomUser in django-allauth (multi type user)
Create an authentication feature with django-allauth and CustomUser in Django
Catch multiple types of exceptions