Issue
I get the following error when I try to signup using Django allauth form with custom user model.
IntegrityError at /accounts/signup/
NOT NULL constraint failed: users_user.birth_date
This is the custom user model and its manager I use:
from django.contrib.auth.models import AbstractUser, BaseUserManager ## A new class is imported. ##
from django.db import models
from datetime import datetime
class UserManager(BaseUserManager):
"""Model manager for User model with no username field."""
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
"""Make and save a User with the given email and password."""
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
"""Make and save a regular User with the given email and password."""
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
"""Make and save a SuperUser with the given email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
# User model
class User(AbstractUser):
"""User model."""
username = None
email = models.EmailField(verbose_name='email address', unique=True)
first_name = models.CharField(max_length=128, blank=False)
last_name = models.CharField(max_length=128, blank=False)
profession_title = models.CharField(max_length=128, blank=False)
country = models.CharField(max_length=32, blank=False)
city = models.CharField(max_length=64, blank=False)
postal_code = models.CharField(max_length=24, blank=False)
address = models.CharField(max_length=255, blank=False)
workplace = models.CharField(max_length=128, blank=False)
phone_nr = models.CharField(max_length=32, blank=False)
birth_date = models.DateField(null=False, blank=True)
email_verified = models.BooleanField(default=False) # email verified
activation_key = models.CharField(max_length=64)
key_expires = models.DateTimeField(blank=False, default=datetime.now())
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'profession_title',
'country', 'city', 'postal_code','address',
'workplace', 'phone_nr', 'birth_date']
# Email & Password are required by default.
@property
def is_email_verified(self):
"Is the user email verified?"
return self.email_verified
This is the form I use:
class CustomSignupForm(SignupForm):
first_name = forms.CharField(label='First name', widget=forms.TextInput, required=True, max_length=128)
last_name = forms.CharField(label='Last name', widget=forms.TextInput, required=True, max_length=128)
profession_title = forms.CharField(label='Profession name', widget=forms.TextInput, required=True, max_length=128)
country = forms.CharField(label='Country', widget=forms.TextInput, required=True, max_length=32)
city = forms.CharField(label='City', widget=forms.TextInput, required=True, max_length=64)
postal_code = forms.CharField(label='Postal code', widget=forms.TextInput, required=True, max_length=24)
address = forms.CharField(label='Address', widget=forms.TextInput, required=True, max_length=255)
workplace = forms.CharField(label='Workplace', widget=forms.TextInput, required=True, max_length=128)
phone_nr = forms.CharField(label='Phone number', widget=forms.TextInput, required=True, max_length=32)
birth_date = forms.DateField(label='Birth date', widget=forms.SelectDateWidget(years=range(1900, datetime.now().year)), required=True)
is_staff = forms.BooleanField(label='Is admin?', widget=forms.CheckboxInput, required=False)
def __init__(self, *args, **kwargs):
super(CustomSignupForm, self).__init__(*args, **kwargs)
del self.fields['password2']
del self.fields['password1']
def signup(self, request, user):
self.cleaned_data['password1'] = 'unknown'
print("Birth date", self.cleaned_data['birth_date'])
user = super(CustomSignupForm, self).save(commit=False)
password = User.objects.make_random_password()
user.set_password(password)
user.save()
return user
class Meta:
model = User
fields = ('email', 'first_name', 'last_name',
'profession_title','country', 'city', 'postal_code','address',
'is_staff','workplace', 'phone_nr', 'birth_date',)
The error message I get looks like this:
Quit the server with CONTROL-C.
[28/Mar/2022 13:31:57] "GET / HTTP/1.1" 200 1301
[28/Mar/2022 13:31:59] "GET /accounts/login/ HTTP/1.1" 200 1348
[28/Mar/2022 13:32:01] "GET /accounts/signup/ HTTP/1.1" 200 8241
/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/fields/__init__.py:1534: RuntimeWarning: DateTimeField User.key_expires received a naive datetime (2022-03-28 13:31:53.714715) while time zone support is active.
warnings.warn(
Internal Server Error: /accounts/signup/
Traceback (most recent call last):
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: users_user.birth_date
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/views/generic/base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/decorators.py", line 20, in wrap
resp = function(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/views/decorators/debug.py", line 92, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/views.py", line 234, in dispatch
return super(SignupView, self).dispatch(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/views.py", line 77, in dispatch
response = super(RedirectAuthenticatedUserMixin, self).dispatch(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/views.py", line 207, in dispatch
return super(CloseableSignupMixin, self).dispatch(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/views/generic/base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/views.py", line 105, in post
response = self.form_valid(form)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/views.py", line 250, in form_valid
self.user = form.save(self.request)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/forms.py", line 423, in save
adapter.save_user(request, user, self)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/allauth/account/adapter.py", line 250, in save_user
user.save()
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 68, in save
super().save(*args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 806, in save
self.save_base(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 857, in save_base
updated = self._save_table(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 1000, in _save_table
results = self._do_insert(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/base.py", line 1041, in _do_insert
return manager._insert(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/query.py", line 1434, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1621, in execute_sql
cursor.execute(sql, params)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 103, in execute
return super().execute(sql, params)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/asdf/python-projects/medimageshub/.venv/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: users_user.birth_date
[28/Mar/2022 13:32:06] "POST /accounts/signup/ HTTP/1.1" 500 219583
birth_date should be a required field in the database.
I tried to assign the birth_date right before user.save like
this:
def signup(self, request, user):
self.cleaned_data['password1'] = 'unknown'
print("Birth date", self.cleaned_data['birth_date'])
user = super(CustomSignupForm, self).save(commit=False)
password = User.objects.make_random_password()
user.set_password(password)
user.birth_date = self.cleaned_data['birth_date']
user.save()
return user
But I still get the same error.
It only is fixed when I remove the "birth_date" from the table and migrate. But this is not the solution I want as I want to have a birth_date field which is required.
I tried to debug, but I can’t see what’s inside cleaned_data set.
I can’t figure out why I get this error.
How do I fix this error?
Solution
I fodund why. I assumed it saved all other data at first, but it didn’t. It saves no data. I followed some allauth guide online and used def signup()
, I should just use def save()
. I found the solution online:
Django allauth: Custom SignUp form doesn't save all of the fields
Answered By – Coder88
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0