4
\$\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.

Toby Speight
87.1k14 gold badges104 silver badges322 bronze badges
asked Jan 14, 2022 at 21:13
\$\endgroup\$
1
  • 5
    \$\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\$ Commented Jan 15, 2022 at 12:53

1 Answer 1

2
\$\begingroup\$

According to my limited knowledge, You stated that,

"...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..."

  • So, most of your fields are set to null=True, blank=True. if blank=True those fields for admin and custom forms will not be required, whereas if it is False the fields cannot be blank. Required fields need to be identified.

  • For covid19_vaccination_card_photo you haven't set a path to upload. So, maintaining a well-structured folder structure might be a good idea.

  • And check your CustomUserManager implementation too.

answered Apr 1, 2022 at 13:31
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.