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.
JavaScript
x
2
1
django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_profile.date_of_birth
2
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
JavaScript
1
83
83
1
from django.contrib.auth.models import User
2
from django.db import models
3
4
5
class Language(models.Model):
6
# - [ CHOICES ] - #
7
name = models.CharField(max_length=20)
8
9
# - [ METHODS ] - #
10
def __str__(self):
11
return self.name
12
13
14
class Skill(models.Model):
15
# - [ CHOICES ] - #
16
name = models.CharField(max_length=20)
17
18
# - [ METHODS ] - #
19
def __str__(self):
20
return self.name
21
22
23
class Profile(models.Model):
24
# - [ CHOICES ] - #
25
GENDER_CHOICES = [
26
('Female', 'Female'),
27
('Male', 'Male')
28
]
29
EDUCATIONAL_LEVEL_CHOICES = [
30
('None', 'None'),
31
('Primary School', 'Primary School'),
32
('Secondary School', 'Secondary School'),
33
('High School', 'High School'),
34
('Bachelor's Degree', 'Bachelor's Degree'),
35
('Master's Degree', 'Master's Degree'),
36
('PhD', 'PhD')
37
]
38
MEDICAL_CONDITIONS_CHOICES = [
39
('None', 'None'),
40
('Chronic Disease', 'Chronic Disease'),
41
('Allergies', 'Allergies'),
42
('COVID-19', 'COVID-19')
43
]
44
ROLES_CHOICES = [
45
(1, 'Volunteer'),
46
(2, 'Content Editor'),
47
(3, 'Board Member'),
48
(4, 'Founder')
49
]
50
51
# - [ FIELDS ] - #
52
user = models.OneToOneField(User, on_delete=models.CASCADE)
53
first_name = models.CharField(max_length=20, blank=False)
54
middle_name = models.CharField(max_length=20)
55
last_name = models.CharField(max_length=20, blank=False)
56
date_of_birth = models.DateField()
57
gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
58
primary_phone_number = models.CharField(max_length=15)
59
backup_phone_number = models.CharField(max_length=15, blank=True)
60
street_address = models.CharField(max_length=40)
61
educational_level = models.CharField(max_length=20, choices=EDUCATIONAL_LEVEL_CHOICES)
62
medical_condition = models.CharField(max_length=15, choices=MEDICAL_CONDITIONS_CHOICES)
63
role = models.PositiveSmallIntegerField(default=1, choices=ROLES_CHOICES)
64
occupation = models.CharField(max_length=40)
65
biography = models.TextField(max_length=500, blank=True)
66
kokar_points = models.PositiveIntegerField(default=0)
67
is_email_verified = models.BooleanField(default=False)
68
is_subscribed_to_newsletter = models.BooleanField(default=True)
69
is_vaccinated = models.BooleanField(default=False)
70
profile_photo = models.ImageField(upload_to='profile_photos/')
71
id_photo = models.ImageField(upload_to='id_photos/')
72
vaccination_card_photo = models.ImageField(upload_to='vaccination_card_photos/', blank=True, null=True)
73
languages = models.ManyToManyField(Language)
74
skills = models.ManyToManyField(Skill)
75
76
# - [ METHODS ] - #
77
def __str__(self):
78
return self.get_full_name()
79
80
def get_full_name(self):
81
return f'{self.first_name} {self.middle_name} {self.last_name}'
82
83
- signals.py
JavaScript
1
18
18
1
from django.db.models.signals import post_save
2
from django.contrib.auth.models import User
3
from django.dispatch import receiver
4
5
from .models import Profile
6
7
8
@receiver(post_save, sender=User)
9
def create_user_profile(sender, instance, created, **kwargs):
10
if created:
11
Profile.objects.create(user=instance)
12
13
14
@receiver(post_save, sender=User)
15
def save_user_profile(sender, instance, **kwargs):
16
instance.profile.save()
17
18
Advertisement
Answer
I have solved this by providing a default value for the date_of_birth field.