运行 "createsuperuser" 时 SQLite3 完整性错误

SQLite3 Integrity Error When Running "createsuperuser"

我正在尝试通过 OneToOneField.

链接到它来扩展默认的 Django User 模型

我成功迁移了更改并在 admin.py 中注册了我的 Profile 模型,但是,当我尝试 运行 命令 python manage.py createsuperuser 并填写信息 我得到一个 完整性错误

django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_profile.date_of_birth

我知道问题出在哪里。我有一个名为 date_of_birth 的字段,这是必需的,我不能将其留空,这会引发异常。

我想要一个简单的解决方案,但想不出一个,而且我不想在所有必填字段中添加一堆 blank=True 片段。

这是我的代码。

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}'

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()

您需要删除迁移文件并再次运行 python manage.py makemigrations 命令。当它提示一次性默认值时,您可以使用 timezone.now,以填充数据库中预先存在的行。

我已经通过为 date_of_birth 字段提供默认值解决了这个问题。