Allauth 不会保存额外的字段

Allauth will not save additional fields

我一直在尝试扩展我的 allauth 注册表单,但似乎无法正常工作,我没有收到任何错误消息,一切似乎都正常,除了我所有的附加字段都没有被保存。如果你能看出我做错了什么,请告诉我,谢谢!

型号 -

from django.db import models
from django.contrib.auth.models import User

class EmployerProfile(models.Model):

    region_choice = (
        ('1', 'Auckland'),
        ('2', 'Wellington'),
        ('3', 'Christchurch')
    )
    suburb_choice = (
        ('1', 'Glendowie'),
        ('2', 'Kohimarama'),
        ('3', 'Mission Bay')
    )
    industry_choice = (
        ('1', 'Restaurant'),
        ('2', 'IT'),
        ('3', 'Construction')
    )

    user = models.OneToOneField(User, unique=True)
    home_number = models.IntegerField(max_length=12)
    mobile_number = models.IntegerField(max_length=12)
    business_name = models.CharField(max_length=50)
    business_address_number = models.IntegerField()
    business_address_street = models.CharField(max_length=50)
    business_address_region = models.CharField(max_length=50, choices=region_choice)
    business_address_suburb = models.CharField(max_length=50, choices=suburb_choice)
    business_address_postcode = models.IntegerField(max_length=4)
    business_industry = models.CharField(max_length=50, choices=industry_choice)

    class Meta:
        verbose_name = 'Employer Profile'

表格 -

from django import forms
from models import EmployerProfile

class SignupForm(forms.Form):

    first_name = forms.CharField(max_length=30, label='First Name')
    last_name = forms.CharField(max_length=30, label='Last Name')
    home_number = forms.IntegerField(label='Home Number')
    mobile_number = forms.IntegerField(label='Mobile Number')
    business_name = forms.CharField(label='Business Name')
    business_address_number = forms.IntegerField(label='Street Number')
    business_address_street = forms.CharField(label='Street Name')
    business_address_region = forms.ChoiceField(choices=EmployerProfile.region_choice, label='Region')
    business_address_suburb = forms.ChoiceField(choices=EmployerProfile.suburb_choice, label='Suburb')
    business_address_postcode = forms.IntegerField(label='Postcode')
    business_industry = forms.ChoiceField(choices=EmployerProfile.industry_choice, label='Industry')

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.home_number = self.cleaned_data['home_number']
        user.mobile_number = self.cleaned_data['mobile_number']
        user.business_name = self.cleaned_data['business_name']
        user.business_address_number = self.cleaned_data['business_address_number']
        user.business_address_street = self.cleaned_data['business_address_street']
        user.business_address_region = self.cleaned_data['business_address_region']
        user.business_address_suburb = self.cleaned_data['business_address_suburb']
        user.business_address_postcode = self.cleaned_data['business_address_postcode']
        user.business_industry = self.cleaned_data['business_industry']

        user.save()

    class Meta:
        model = EmployerProfile

设置-

SITE_ID = 1
ACCOUNT_SIGNUP_FORM_CLASS = 'profiles.forms.SignupForm'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = None
LOGIN_REDIRECT_URL = '/'

您正在为用户对象添加新属性,而不是创建新的 EmployerProfile。 我相信以下代码是您要实现的目标:

class SignupForm(forms.Form):
    # ...
    def signup(self, request, user):
        EmployerProfile.objects.create(
            user=user,
            home_number=self.cleaned_data['home_number'],
            mobile_number=self.cleaned_data['mobile_number'],
            business_name=self.cleaned_data['business_name'],
            business_address_number=self.cleaned_data['business_address_number'],
            business_address_street=self.cleaned_data['business_address_street'],
            business_address_region=self.cleaned_data['business_address_region'],
            business_address_suburb=self.cleaned_data['business_address_suburb'],
            business_address_postcode=self.cleaned_data['business_address_postcode'],
            business_industry=self.cleaned_data['business_industry']
        )