I am trying to extend the default Django User model by linking to it through a OneToOneField.
I successfully migrated the changes and registered my Profile model inside admin.py, however, when I try to run the command python manage.py createsuperuser
and fill in the information I get an Integrity Error.
django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_profile.date_of_birth
I know what the issue is. I have a field called date_of_birth which is required, and I can’t leave it blank, that raises the exception.
I want a simple solution, but couldn’t think of one, and I don’t want to add a bunch of blank=True
snippets to all of my required fields.
Here’s my code.
- models.py
from django.contrib.auth.models import User from django.db import models class Language(models.Model): # - [ CHOICES ] - # name = models.CharField(max_length=20) # - [ METHODS ] - # def __str__(self): return self.name class Skill(models.Model): # - [ CHOICES ] - # name = models.CharField(max_length=20) # - [ METHODS ] - # def __str__(self): return self.name class Profile(models.Model): # - [ CHOICES ] - # GENDER_CHOICES = [ ('Female', 'Female'), ('Male', 'Male') ] EDUCATIONAL_LEVEL_CHOICES = [ ('None', 'None'), ('Primary School', 'Primary School'), ('Secondary School', 'Secondary School'), ('High School', 'High School'), ('Bachelor's Degree', 'Bachelor's Degree'), ('Master's Degree', 'Master's Degree'), ('PhD', 'PhD') ] MEDICAL_CONDITIONS_CHOICES = [ ('None', 'None'), ('Chronic Disease', 'Chronic Disease'), ('Allergies', 'Allergies'), ('COVID-19', 'COVID-19') ] ROLES_CHOICES = [ (1, 'Volunteer'), (2, 'Content Editor'), (3, 'Board Member'), (4, 'Founder') ] # - [ FIELDS ] - # user = models.OneToOneField(User, on_delete=models.CASCADE) first_name = models.CharField(max_length=20, blank=False) middle_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20, blank=False) date_of_birth = models.DateField() gender = models.CharField(max_length=6, choices=GENDER_CHOICES) primary_phone_number = models.CharField(max_length=15) backup_phone_number = models.CharField(max_length=15, blank=True) street_address = models.CharField(max_length=40) educational_level = models.CharField(max_length=20, choices=EDUCATIONAL_LEVEL_CHOICES) medical_condition = models.CharField(max_length=15, choices=MEDICAL_CONDITIONS_CHOICES) role = models.PositiveSmallIntegerField(default=1, choices=ROLES_CHOICES) occupation = models.CharField(max_length=40) biography = models.TextField(max_length=500, blank=True) kokar_points = models.PositiveIntegerField(default=0) is_email_verified = models.BooleanField(default=False) is_subscribed_to_newsletter = models.BooleanField(default=True) is_vaccinated = models.BooleanField(default=False) profile_photo = models.ImageField(upload_to='profile_photos/') id_photo = models.ImageField(upload_to='id_photos/') vaccination_card_photo = models.ImageField(upload_to='vaccination_card_photos/', blank=True, null=True) languages = models.ManyToManyField(Language) skills = models.ManyToManyField(Skill) # - [ METHODS ] - # def __str__(self): return self.get_full_name() def get_full_name(self): return f'{self.first_name} {self.middle_name} {self.last_name}'
- signals.py
from django.db.models.signals import post_save from django.contrib.auth.models import User from django.dispatch import receiver from .models import Profile @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
Advertisement
Answer
I have solved this by providing a default value for the date_of_birth field.