Python - Django signup for custom user model with multiple types of users

心已入冬 提交于 2020-01-05 04:09:08

问题


I'm working on a project using Python(3.7) and Django(2.2) in which I have created multiple types of users with a custom user model by extending the AbstractBaseUser model. I have done with the login but I'm confused about how should I implement the signup page as there's multiple types of users and I need a different form for each type of user.

Here's How I have implemented my models.

From models.py:

class UserManager(BaseUserManager):

    def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
        if not email:
            raise ValueError('Users must have an email address')
        now = timezone.now()
        email = self.normalize_email(email)
        user = self.model(
            email=email,
            is_staff=is_staff,
            is_active=True,
            is_superuser=is_superuser,
            last_login=now,
            date_joined=now,
            **extra_fields
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password, **extra_fields):
        return self._create_user(email, password, False, False, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        user = self._create_user(email, password, True, True, **extra_fields)
        user.save(using=self._db)
        return user


CHOICES = [
    ('M', 'male'),
    ('F', 'female')
]

USER_TYPE = [
    ('PB', 'PersonalBelow18'),
    ('PA', 'PersonalAbove18'),
    ('Parent', 'Parent'),
    ('GC', 'GroupContact'),
]


def generate_cid():
    cid = "".join([random.choice(string.digits) for i in range(8)])
    return cid


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    password = models.CharField(max_length=100)
    # name = models.CharField(max_length=254, null=True, blank=True)
    title = models.CharField(max_length=255, blank=False)
    user_type = models.CharField(max_length=255, choices=USER_TYPE, blank=False)
    gender = models.CharField(max_length=255, choices=CHOICES, blank=False)
    contenst = models.TextField(max_length=500, blank=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = ['password']

    objects = UserManager()

    def get_absolute_url(self):
        return "/users/%i/" % (self.pk)


class PersonalBelow18(User):
    # user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(blank=False)
    customer_id = models.BigIntegerField(blank=False)
    collection_use_personal_data = models.BooleanField(blank=False)


class PersonalAbove18(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    dob = models.DateField(blank=False)
    customer_id = models.BigIntegerField(blank=False)
    contact_email = models.EmailField(blank=False)
    contact_no = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the"
                                                                  "format: '+999999999'. Up to 15 digits allowed.")
    collection_use_personal_data = models.BooleanField(blank=False)


class Parent(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    contact_email = models.EmailField(blank=False)
    contact_no = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the"
                                                                  "format: '+999999999'. Up to 15 digits allowed.")
    collection_use_personal_data = models.BooleanField(blank=False)


class GroupContactPerson(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    contact_email = models.EmailField(blank=False)
    contact_no = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the"
                                                                  "format: '+999999999'. Up to 15 digits allowed.")
    department = models.CharField(max_length=255, blank=False)
    address = models.TextField(max_length=255, blank=False)

What do I want? (the UI perspective): I want to show the type-specific form to the user, thinking to show all the forms on a single page in different tabs, so the user can click on the tab for which he wants to create an account and then I want to show all the fields from User & PersonalBelow18.

How can I implement signup forms & views for these multiple types of users?

来源:https://stackoverflow.com/questions/59246647/python-django-signup-for-custom-user-model-with-multiple-types-of-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!