Here’s my User model:
class UserManager(BaseUserManager):
"""Manager for CustomUser."""
def build_user(self, email, password=None, **extra_fields):
"""Instantiate and return a regular user with an email and password."""
if not email:
raise ValueError(_('The Email field 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 build_superuser(self, email, password, **extra_fields):
"""Instantiate and return a superuser with an email and password."""
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self.create_user(email, password, **extra_fields)
class User(AbstractUser):
"""CustomUser model"""
email = models.EmailField(_('email address'), unique=True, blank=False, null=False)
username = models.CharField(_('profile name'), max_length=150, unique=False, blank=False, null=False)
gender = models.IntegerField(_('gender'), choices=Gender.choices, blank=True, null=True)
birth_date = models.DateField(_('birth date'), blank=True, null=True)
country = CountryField(blank_label='(select country)', blank=True, null=True)
is_admin = models.BooleanField(default=False, blank=True, null=True)
online_status = models.BooleanField(default=False, blank=True, null=True) # True for online, False for offline
USERNAME_FIELD = 'email' # Use email to log in
REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'gender', 'birth_date', 'country'] # 'email' is required by default
objects = UserManager()
def __str__(self):
return self.email
Here’s my SignUpForm class on forms.py
from django import forms
from allauth.account.forms import SignupForm
from .choices import Gender
from django_countries.widgets import CountrySelectWidget
from django_countries import countries
from datetime import datetime
years_range = list(range(datetime.now().year - 18, 1899, -1))
class CustomSignupForm(SignupForm):
first_name = forms.CharField(max_length=30, label='First Name')
last_name = forms.CharField(max_length=30, label='Last Name')
birth_date = forms.DateField(label='Birth Date', widget=forms.DateInput(attrs={'class':'form-control',
'id': "birth_date",
'type':'date'}))
gender = forms.TypedChoiceField(choices=Gender.choices, coerce=int, label='Gender')
country = forms.ChoiceField(choices=countries, widget=CountrySelectWidget(), label='Country')
def save(self, request, user):
# Ensure you call the parent class's save.
# .save() returns a User object.
user = super(CustomSignupForm, self).save(request, user=user)
# Add your own processing here.
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.gender = self.cleaned_data['gender']
user.country = self.cleaned_data['country']
user.birth_date = self.cleaned_data['birth_date']
print(user.first_name)
print(user.last_name)
print(user.gender)
print(user.country)
print(user.birth_date)
user.save()
return user
The error I get is:
TypeError at /accounts/signup/
CustomSignupForm.save() missing 1 required positional argument: 'user'
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/signup/
Django Version: 5.0.2
Exception Type: TypeError
Exception Value:
CustomSignupForm.save() missing 1 required positional argument: 'user'
Exception Location: /home/abka/projects/dating-app-django/.venv/lib/python3.10/site-packages/allauth/account/forms.py, line 403, in try_save
Raised during: allauth.account.views.SignupView
Python Executable: /home/abka/projects/dating-app-django/.venv/bin/python3
Python Version: 3.10.12
And when I see the data base, it doesn’t save the custom fields, only the default fields like email and password.
Has anyone ever used Django-allauth with custom user model in Django 5?
I searched online and on StackOverflow, tried their solutions, but it doesn’t work, this is the result I get. It seems most of them are outdated.
I checked the documentation too.
https://docs.allauth.org/en/latest/account/forms.html
Here they suggest overriding save method. I did it with no results.
I tried the method here too from the documentation:
https://docs.allauth.org/en/latest/account/advanced.html
overriding the save_user method like this:
from allauth.account.adapter import DefaultAccountAdapter
from datetime import date
class MyAccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
user = super(MyAccountAdapter, self).save_user(request, user, form, commit=False)
data = form.cleaned_data
user.birth_date = data['birth_date']
user.gender = data['gender']
user.country = data['country']
user.last_login = date.now()
if commit:
user.save()
return user
But I get the same error.
How do I make this work?
Did anyone ever use Django-allauth with custom user model successfully?