if RegistrationForm.is_valid() and employeeForm.is_valid(): TypeError: is_valid() missing 1 required positional argument: 'self'
我有这个错误,我找不到来源。我错过了什么?
@transaction.atomic
def create_view(request):
if request.method == 'POST':
user_form = RegistrationForm(request.POST)
profile_form = employeeForm(request.POST or None,request.FILES or None)
if RegistrationForm.is_valid() and employeeForm.is_valid():
user = RegistrationForm.save()
user.refresh_from_db()
profile_form = employeeForm(request.POST, instance=user.employeemodel)
profile_form.full_clean()
profile_form.save()
return redirect('emp_list')
else:
user_form = RegistrationForm()
profile_form = employeeForm()
return render(request,'create_employee.html',{'userform':user_form,'profileform':profile_form})
答案 0 :(得分:2)
在使用表单时,应使用实例对象而不是类对象。所以它应该是user_form.is_valid() and profile_form.is_valid()
user_form = RegistrationForm(request.POST)
profile_form = employeeForm(request.POST or None,request.FILES or None)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.refresh_from_db()
profile_form = employeeForm(request.POST, instance=user.employeemodel)
profile_form.full_clean()
profile_form.save()
return redirect('emp_list')
save()
方法相同:user = user_form.save()