Django Allauth自定义注册表单问题:cpf_cnpj字段无法保存至用户模型的解决方法
cpf_cnpj Field with Django Allauth Signup Form Hey there! Let's break down why your cpf_cnpj field isn't saving, and how to fix it:
The Root Cause
Your default Django User model doesn't include a cpf_cnpj field—this is why first_name and last_name work (they're built-in fields), but your custom field gets ignored. You have two solid options to resolve this:
Option 1: Extend the Django User Model (Recommended for New Projects)
If you're early in your project, replacing the default User model with a custom one is the cleanest approach:
- Create a Custom User Model in
models.py:
from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): # Add your custom field here cpf_cnpj = models.CharField(max_length=14)
- Update Settings to use your new model in
settings.py:
# Replace 'mysite' with your app name AUTH_USER_MODEL = 'mysite.CustomUser'
- Run Migrations to apply the changes to your database:
python manage.py makemigrations python manage.py migrate
- Fix Your Form's Save Method (note the typo correction!):
class MyCustomSignupForm(SignupForm): cpf_cnpj = forms.CharField(max_length=14) first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) def save(self, request): user = super(MyCustomSignupForm, self).save(request) # Fixed the typo: cleanedD_data → cleaned_data user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.cpf_cnpj = self.cleaned_data['cpf_cnpj'] user.save() return user
Option 2: Use a Separate Profile Model (Good for Existing Projects)
If you can't replace the default User model (e.g., your project is already in progress), create a one-to-one linked profile model:
- Create a Profile Model in
models.py:
from django.db import models from django.contrib.auth.models import User class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) cpf_cnpj = models.CharField(max_length=14)
- Update Your Form's Save Method to create/update the profile:
class MyCustomSignupForm(SignupForm): cpf_cnpj = forms.CharField(max_length=14) first_name = forms.CharField(max_length=30) last_name = forms.CharField(max_length=30) def save(self, request): user = super(MyCustomSignupForm, self).save(request) # Fix the typo here too! user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.save() # Create or get the existing profile for the user profile, created = UserProfile.objects.get_or_create(user=user) profile.cpf_cnpj = self.cleaned_data['cpf_cnpj'] profile.save() return user
- Run Migrations to create the profile table:
python manage.py makemigrations python manage.py migrate
Quick Note: Typo Fix!
I noticed you had cleanedD_data (capital D) in your original code for last_name—that's a typo! Even if you said last_name was saving, fixing that will ensure it works reliably.
内容的提问来源于stack exchange,提问作者Jorge Marques

