1
\$\begingroup\$

This is my second Django project for an actual client and I want some review on one of my important models.

This model will be responsible for authenticating users using their E-Mail Addresses and Passwords. At the same time it will store all of the details for each specific user which later will be used to identify who's who and give specific permissions to a specific user group.

This custom user model exists in most of the projects I do, so I want to perfect it.

from phonenumber_field.modelfields import PhoneNumberField
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractUser
from .managers import CustomUserManager
from django.db import models


class CustomUser(AbstractUser):
    """
    Custom Kokar User Model.
    """
    GENDER_CHOICES = [
        (0, _('Female')),
        (1, _('Male'))
    ]
    MEDICAL_CONDITION_CHOICES = [
        (0, _('None')),
        (1, _('Chronic Disease')),
        (2, _('Allergies'))
    ]
    EDUCATIONAL_LEVEL_CHOICES = [
        (0, _('None')),
        (1, _('Primary School')),
        (2, _('Secondary School')),
        (3, _('High School')),
        (4, _('Institute Diploma')),
        (5, _('Bachelor\'s Degree')),
        (6, _('Master\'s Degree')),
        (7, _('PhD'))
    ]

    username = None
    email = models.EmailField(_('E-Mail Address'), unique=True)
    middle_name = models.CharField(_('Middle Name'), max_length=150)
    gender = models.IntegerField(_('Gender'), choices=GENDER_CHOICES, blank=True, null=True)
    birth_date = models.DateField(_('Birth Date'), blank=True, null=True)
    primary_phone = PhoneNumberField(_('Primary Phone Number'), blank=True, null=True)
    backup_phone = PhoneNumberField(_('Backup Phone Number'), blank=True, null=True)
    street_address = models.CharField(_('Street Address'), max_length=50, blank=True, null=True)
    occupation = models.CharField(_('Occupation'), max_length=30, blank=True, null=True)
    profile_photo = models.ImageField(_('Profile Photo'), upload_to='profile_photos/', blank=True, null=True)
    id_photo = models.ImageField(_('ID Photo'), upload_to='id_photos/', blank=True, null=True)
    covid19_vaccination_card_photo = models.ImageField(_('COVID-19 Vaccination Card Photo'), blank=True, null=True)
    medical_condition = models.IntegerField(_('Medical Condition'), choices=MEDICAL_CONDITION_CHOICES, blank=True, null=True)
    educational_level = models.IntegerField(_('Educational Level'), choices=EDUCATIONAL_LEVEL_CHOICES, blank=True, null=True)
    is_subscribed_to_newsletter = models.BooleanField(_('Is Subscribed to Newsletter'), default=True)
    kokar_points = models.IntegerField(_('Kokar Points'), default=0)
    languages = models.JSONField(_('Languages'))
    skills = models.JSONField(_('Skills'))

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['middle_name']

    objects = CustomUserManager()

    def __str__(self):
        return self.email
    
    def get_full_name(self):
        return f'{self.first_name} {self.middle_name} {self.last_name}'

You might see a reference to the word Kokar at a couple of spots, that's the name of the client / project I am working on.

New contributor
saadsawash is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
1
  • \$\begingroup\$ Not about the code, but be aware that in most jurisdictions, health information is subject to even stricter safeguards than other personal data, so make sure your client's Data Controller is aware of what you're doing. \$\endgroup\$ yesterday

Your Answer

saadsawash is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.