You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django Allauth自定义注册表单问题:cpf_cnpj字段无法保存至用户模型的解决方法

How to Save Custom 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:


If you're early in your project, replacing the default User model with a custom one is the cleanest approach:

  1. 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)
  1. Update Settings to use your new model in settings.py:
# Replace 'mysite' with your app name
AUTH_USER_MODEL = 'mysite.CustomUser'
  1. Run Migrations to apply the changes to your database:
python manage.py makemigrations
python manage.py migrate
  1. 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:

  1. 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)
  1. 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
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:37:35