我正在嘗試通過OneToOneField鏈接到默認的 Django User模型來擴展它。
我成功遷移了更改并在admin.py中注冊了我的Profile模型,但是,當我嘗試運行命令并填寫資訊時,我得到一個Integrity Error。python manage.py createsuperuser
django.db.utils.IntegrityError: NOT NULL constraint failed: accounts_profile.date_of_birth
我知道問題是什么。我有一個名為date_of_birth的欄位,它是必需的,我不能將其留空,這會引發例外。
我想要一個簡單的解決方案,但想不出一個,而且我不想blank=True在所有必填欄位中添加一堆片段。
這是我的代碼。
- 模型.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}'
- 信號.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()
uj5u.com熱心網友回復:
您需要洗掉遷移檔案并再次運行該python manage.py makemigrations命令。當它提示一次性默認值時,您可以使用 timezone.now,以填充資料庫中預先存在的行。
uj5u.com熱心網友回復:
我通過為date_of_birth欄位提供默認值解決了這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410119.html
標籤:
