What am I doing wrong?
Django Models:
USER MODEL
class User(AbstractUser):
# DEFINE MODEL FIELDS
related_person = models.OneToOneField(Person, on_delete=models.CASCADE, default='')
user_id = models.BigAutoField(verbose_name='User ID', primary_key=True, serialize=False, auto_created=True)
email = models.EmailField(verbose_name='Email', max_length=80, blank=False, unique=True, null=False, default='')
is_verified = models.BooleanField(verbose_name='Email Verified', blank=False, default=False)
date_joined = models.DateField(verbose_name='date joined', auto_now_add=True, null=True)
last_login = models.DateField(verbose_name='last login', auto_now=True, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = None
last_name = None
username = None
# IMPORT MODEL MANAGER CLASS
objects = UserManager()
# SET LOGIN FIELD
USERNAME_FIELD = 'email'
# SET REQUIRED FIELDS
REQUIRED_FIELDS = ['is_verified']
PERSON MODEL
class Person(models.Model):
class Titles(models.TextChoices):
MR = 'Mr', _('Mr')
MRS = 'Mrs', _('Mrs')
MISS = 'Miss', _('Miss')
MS = 'Ms', _('Ms')
DR = 'Dr', _('Dr')
related_entity = models.OneToOneField(Entity, on_delete=models.CASCADE)
person_id = models.BigAutoField(verbose_name='Person ID', primary_key=True, serialize=False, auto_created=True)
first_name = models.CharField(verbose_name='First Name', max_length=50, blank=False, null=False)
last_name = models.CharField(verbose_name='Last Name', max_length=50, blank=False, null=False)
title = models.CharField(verbose_name='Title', max_length=4, choices=Titles.choices, blank=True, null=False, default='')
alias = models.CharField(verbose_name='Alias', max_length=150, blank=True, null=False)
ENTITY MODEL
class Entity(models.Model):
class EntityTypes(models.TextChoices):
PERSON = 'Person', _('Person')
COMPANY = 'Company', _('Company')
entity_type = models.CharField(
verbose_name='Legal Entity Type',
max_length=7,
choices=EntityTypes.choices,
default=EntityTypes.PERSON,
blank=False,
null=False)
I have a Sign Up form that saves a person's email to the User Model, and password to the User Model, after checking the email doesn't already exist, and passwords (when entered twice), match. but then I get this error when I save the form:
null value in column "related_person_id" of relation "customauth_user" violates not-null constraint
DETAIL: Failing row contains (1, pbkdf2_sha256$320000$Ipsu5kX2Opd2a1j9996ReS$uPR4GCbSxS7 9 0KIAaE..., [email protected], f, 2022-02-28, 2022-02-28, t, f, f, f, null).
Any ideas?
The overall idea behind the models was to allow a someone to signup with an email and password, through the User model. Then once they've logged in, they can enter their firstname, lastname, title, alias, etc, in the Person model.
CodePudding user response:
Your User-Model does not allow null to be set as value for the column related_person
(Django's ORM transpiles this to related_person_id
in SQL)
By default this causes SQL to put a Null-Constraint on that column.
However you can prevent Django from implementing such a Constraint, by simply parsing null = True
as additional argument to the affected column of your Model.
Thus you can simply change your Models to:
class User(AbstractUser):
# DEFINE MODEL FIELDS
related_person = models.OneToOneField(Person, on_delete=models.CASCADE, default='', null=True) # allowing null-values
user_id = models.BigAutoField(verbose_name='User ID', primary_key=True, serialize=False, auto_created=True)
email = models.EmailField(verbose_name='Email', max_length=80, blank=False, unique=True, null=False, default='')
is_verified = models.BooleanField(verbose_name='Email Verified', blank=False, default=False)
date_joined = models.DateField(verbose_name='date joined', auto_now_add=True, null=True)
last_login = models.DateField(verbose_name='last login', auto_now=True, null=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
first_name = None
last_name = None
username = None
# IMPORT MODEL MANAGER CLASS
objects = UserManager()
# SET LOGIN FIELD
USERNAME_FIELD = 'email'
# SET REQUIRED FIELDS
REQUIRED_FIELDS = ['is_verified']
# ...