gpt4 book ai didi

python - 完整性错误 *_id 不能为空

转载 作者:行者123 更新时间:2023-12-01 05:17:13 25 4
gpt4 key购买 nike

我对 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)

如您所见,我没有手动尝试设置表单集的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

最佳答案

表单集是一组表单。它没有申请人 ID。尝试设置一个没有意义。

实际上,您几乎已经在做正确的事情了。在 GET block 中,您正确地将 instance 参数传递给表单集实例化,以将其与申请人关联。但是您没有在 POST block 中执行此操作,您应该:

    form = ApplicantForm(request.POST, instance=applicant)
formset = EducationInlineFormSet(request.POST, instance=applicant)

关于python - 完整性错误 *_id 不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com