我一直在尝试扩展我的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 = '/'
答案 0 :(得分:0)
您正在添加用户对象的新属性,而不是创建新的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']
)