我对Python和Django比较陌生,所以请原谅我的无知。
保存表单集时收到以下错误
IntegrityError at /jobs/1/
jobs_education.applicant_id may not be NULL
以下是我的观点:
def application(request, job_id):
job = get_object_or_404(Job, pk=job_id)
#return 404 if job isn't yet published
if (job.pub_date>timezone.now()):
return HttpResponseNotFound('<h1>Job not found</h1>')
EducationInlineFormSet = inlineformset_factory(Applicant, Education, extra=1, can_delete=False)
if request.method == 'POST':
form = ApplicantForm(request.POST)
formset = EducationInlineFormSet(request.POST)
if form.is_valid() and formset.is_valid():
# save the model to database, directly from the form:
applicant_saved = form.save()
formset.applicant_id = applicant_saved.id
print 'formset %s' % formset.__dict__
formset.save()
return render(request, 'jobs/success.html')
else:
applicant = Applicant(job=job)
form = ApplicantForm(instance=applicant)
formset = EducationInlineFormSet(instance=applicant)
c = {
'form' : form ,
'formset' : formset,
}
return render(request, 'jobs/test.html', c)
正如您所看到的,我不是手动尝试设置formset的applicant_id,但它没有什么区别。
输出:
print 'formset %s' % formset.__dict__
是
{'auto_id': u'id_%s', 'is_bound': True, 'initial_extra': None, 'error_class': <class 'django.forms.util.ErrorList'>, 'save_as_new': False, '_non_form_errors': [], 'initial': None, 'queryset': [], '_pk_field': <django.db.models.fields.AutoField: id>, 'forms': [<django.forms.models.EducationForm object at 0x1014078d0>], 'instance': <Applicant: >, 'prefix': u'education_set', 'applicant_id': 4, 'data': <QueryDict: {u'education_set-0-date': [u'1989-05-19'], u'education_set-0-school': [u'asdf'], u'education_set-0-grade': [u'oi'], u'telephone': [u'mlk'], u'nationality': [u'lk'], u'address_postcode': [u'lk'], u'address_line2': [u'lkm'], u'address_line1': [u'm'], u'education_set-MAX_NUM_FORMS': [u'1000'], u'applicant_dob': [u'1989-05-14'], u'address_district': [u'lkm'], u'csrfmiddlewaretoken': [u'3tfdnCwqYSWkyoTjEBX6peUVCGRANSKj'], u'email': [u'lkm@c.com'], u'national_insurance_number': [u'mlk'], u'address_county': [u'lkm'], u'job': [u'1'], u'address_town': [u'lkm'], u'education_set-INITIAL_FORMS': [u'0'], u'education_set-0-town': [u'sdf'], u'education_set-TOTAL_FORMS': [u'1'], u'applicant_name': [u'asd'], u'education_set-0-applicant': [u''], u'mobile': [u'm'], u'education_set-0-id': [u''], u'applicant_surname': [u'klm'], u'education_set-0-qualification': [u'oj']}>, '_errors': [{}], 'files': {}}
如果我还能为您提供其他任何信息,请与我们联系。
提前致谢,
克里斯
编辑:
在这里添加我的模型,因为有人必须要求他们
class Location(models.Model):
location_name = models.CharField(max_length=50)
def __str__(self):
return self.location_name
class Job(models.Model):
location = models.ForeignKey(Location)
job_title = models.CharField(max_length=50)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.job_title
class Applicant(models.Model):
job = models.ForeignKey(Job)
applicant_name = models.CharField(max_length=50)
applicant_surname = models.CharField(max_length=50)
applicant_dob = models.DateField('Date of Birth')
nationality = models.CharField(max_length=50)
national_insurance_number=models.CharField(max_length=20)
address_line1=models.CharField(max_length=50)
address_line2=models.CharField(max_length=50)
address_district=models.CharField(max_length=50)
address_town=models.CharField(max_length=50)
address_county=models.CharField(max_length=50)
address_postcode=models.CharField(max_length=50)
telephone=models.CharField(max_length=12)
mobile=models.CharField(max_length=12)
email=models.EmailField(max_length=50)
approved=models.BooleanField(default=False)
def __str__(self):
return self.applicant_name
def isApproved(self):
return self.approved
def approve(self):
self.approved = True
class Education(models.Model):
applicant = models.ForeignKey(Applicant)
school=models.CharField(max_length=50)
town=models.CharField(max_length=50)
date=models.DateField('date')
qualification=models.CharField(max_length=50)
grade=models.CharField(max_length=50)
def __str__(self):
return self.school
答案 0 :(得分:2)
formset是一组表单。它没有申请人ID。试图设置一个没有意义。
你实际上几乎已经做了正确的事情。在GET块中,您正确地将instance
参数传递给formset实例,以将其与申请人关联。但是你在POST块中没有这样做,你应该是:
form = ApplicantForm(request.POST, instance=applicant)
formset = EducationInlineFormSet(request.POST, instance=applicant)