我扩展了Django用户模型并添加了我自己的字段,目前正在注册填写这些字段。表格似乎工作正常,除了保存之外的一切。
我用this来帮助我。
以下是用户模型的扩展名:
public function page_one() {
$options = array(
array( 'content1', 'Content1', 'The Content 1'),
array( 'content2', 'Content2', 'The Content 2'),
array( 'content3', 'Content3', 'The Content 3'),
);
$this->add_settings( 'page_one', $options );
return $options;
}
public function generate_scripts() {
$options = $this->page_one();
foreach($options as $option) {
echo $option[0];
}
}
这是我的表格:
class StudentProfile(models.Model):
user = models.OneToOneField(User, null = True, related_name='user', on_delete=models.CASCADE)
teacher = models.BooleanField(default = False)
school = models.CharField(max_length = 50)
def create_StudentProfile(sender, **kwargs):
if kwargs['created']:
user_profile = StudentProfile.objects.create(user = kwargs['instance'])
post_save.connect(create_StudentProfile, sender = User)
以下是我的观点:
class StudentRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
school = forms.CharField(required = True)
def __init__(self, *args, **kwargs):
super(StudentRegistrationForm, self).__init__(*args, **kwargs)
self.fields['username'].help_text = ''
self.fields['password2'].help_text = ''
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'school',
'password1',
'password2'
)
def save(self, commit = True):
user = super(StudentRegistrationForm, self).save(commit = False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
student_profile = StudentProfile(user = user, school = self.cleaned_data['school'])
if commit:
user.save()
student_profile.save()
return user, student_profile
这是我的追溯:
def registration(request):
if request.method == 'POST':
form = StudentRegistrationForm(request.POST)
if form.is_valid():
user, user_profile = form.save(commit = False)
form.save()
return render(request, 'accounts/home.html')
else:
args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
else:
form = StudentRegistrationForm()
args = {'form': form}
return render(request, 'accounts/reg_form.html', args)
谢谢!
答案 0 :(得分:1)
您必须使用User
模型创建用户,然后您必须将此用户传递给StudentProfile
,因为它是onetoone
字段StudentProfile
。
def save(self, request):
form = StudentRegistrationForm(request.POST)
user = User.objects.create(first_name=form.cleaned_data['first_name'],
last_name=form.cleaned_data['last_name'],
email=form.cleaned_data['email'],
username=form.cleaned_data['username'])
user_profile = StudentProfile.objects.create(user=user,
teacher=form.cleaned_data['teacher'],
school=form.cleaned_data['school'])
return user, student_profile
答案 1 :(得分:0)
看起来似乎没有保存use对象,因此它可以存储其引用的StudentProfile模型。
答案 2 :(得分:0)
这里有很多问题。
当前的问题是你在两个地方创建了UserProfile。您已经注册了一个信号处理程序 - create_StudentProfile
- 在创建用户时自动创建一个;但你也在用save
方法创建一个新方法。
您应该考虑是否真的需要该信号处理程序。如果您总是要通过此表单创建用户,则不需要该处理程序。
如果您确定需要信号,那么您需要重做保存方法以考虑它。类似的东西:
def save(self, commit=True):
user = super(StudentRegistrationForm, self).save(commit = False)
if commit:
user.save()
profile = user.userprofile
else:
profile = UserProfile(user=user)
profile.school = self.cleaned_data['school']
if commit:
profile.save()
return user, student_profile
您会注意到无需设置电子邮件和姓名,因为这些是通过超级电话中已有的表单完成的。
第二个问题是您在视图中两次调用save方法。不要那样做。
if form.is_valid():
user, user_profile = form.save()
return render(request, 'accounts/home.html')