Formset.is_valid()错,为什么?

时间:2016-03-04 21:59:26

标签: python django validation formset

我试图验证表单集,它在模板中完美显示但是当我尝试保存它时,其验证返回False。

我试图将一些错误回调和print(formset.non_form_errors)中的错误说成以下内容:

<bound method PhysicianSpecialtiesFormFormSet.non_form_errors of <django.forms.formsets.PhysicianSpecialtiesFormFormSet object at 0x7fd70af38a10>>

我不知道如何在表格中输入正确的内容;表单是一个下拉列表,它不可能出错。

def UpdatePhysician(request,id):

physician = get_object_or_404(Physician, id=id)
employee = get_object_or_404(Employee, id=physician.employee.id)
person = get_object_or_404(Person, id=employee.person.id)
try:
    address = get_object_or_404(Address, id=return_id(str(person.addresses.values('id'))))
except:
    address = None
try:
    email = get_object_or_404(Email, id=return_id(str(person.emails.values('id'))))
except:
    email = None
try:
    phone = get_object_or_404(PhoneNumber, id=return_id(str(person.phone_numbers.values('id'))))
except:
    phone = None
try:
    academic_degree = get_object_or_404(AcademicDegree, id=return_id(str(employee.academic_degrees.values('id'))))
    university = get_object_or_404(University, id=return_id(str(employee.academic_degrees.values('university'))))
except:
    academic_degree = None
    university = None

physician_specialties = get_list_or_404(PhysicianSpecialties, employee_academic_degree__employee__pk=physician.employee.id)
for item in physician_specialties:
    print(unicode(item.specialty))
specialties = Specialty.objects.all()
specialty = PhysicianSpecialties.objects.filter(employee_academic_degree__employee__physician__pk=id)


SpecialtyFormset = modelformset_factory(PhysicianSpecialties, fields=('specialty',),max_num=1, labels=None, )
formset = SpecialtyFormset(queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id))
person_form = PersonForm(request.POST or None, instance=person)
employee_form = EmployeeForm(request.POST or None, instance=employee)
physician_form = PhysicianForm(request.POST or None, instance=physician)
email_form = EmailForm(request.POST or None, instance=email)
address_form = AddressForm(request.POST or None, instance=address)
phone_form = PhoneForm(request.POST or None, instance=phone)
academic_degree_form = AcademicDegreeForm(request.POST or None, instance= academic_degree)
university_form = UniversityForm(request.POST or None, instance=university)



if request.method == "POST":

    if (person_form.is_valid() and employee_form.is_valid() and physician_form.is_valid() and email_form.is_valid() and
            address_form.is_valid() and phone_form.is_valid and physician_form.is_valid() and university_form.is_valid and
            academic_degree_form.is_valid() and formset.is_valid()):
        person_form.save()
        physician_form.save()
        new_address = address_form.save()
        new_email = email_form.save()
        new_phone = phone_form.save()
        # academic_degree_form.save()
        # university_form.save()
        formset.save()

        if address == None:
            PersonAddress.objects.create(person=person, address=new_address)

        if email == None:
            PersonEmail.object.create(person=person, email=new_email)

        if phone == None:
            PersonPhoneNumber.objects.create(person=person, hone_number=new_phone)

        return HttpResponse('yesh')
    else:
        print(formset.is_valid())
        print(formset.non_form_errors)
        print('nope')
        return HttpResponse('nope')

1 个答案:

答案 0 :(得分:2)

您尚未将数据传递到您的formset。如果没有发布数据,您的formset将不受约束,并且始终无效。

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtpout.europe.secureserver.net"; // SMTP server
$mail->SMTPDebug  = 2;  // enables SMTP debug information (for testing)

mail($my_email,$subject,$message,$headers);

另一个问题是您尝试错误地显示错误。 formset = SpecialtyFormset(request.POST or None, queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id)) 是一种方法,您必须将其称为。<​​/ p>

formset.non_form_errors

表单中的表单可能还有错误,您可以通过打印print(formset.non_form_errors()) 来查看这些错误。

formset.errors