python - Integrity Error *_id may not be null -
i'm relatively new python , django please forgive ignorance.
i receiving following error when saving formset
integrityerror @ /jobs/1/ jobs_education.applicant_id may not null
here view:
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() , formset.is_valid(): # save model database, directly 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)
as can see, not manually trying set applicant_id of formset isn't making difference.
the output of:
print 'formset %s' % formset.__dict__
is
{'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 @ 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': {}}
please let me know if there else can provide with.
thanks in advance,
chris
edit:
adding models here bound ask them
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
a formset set of forms. doesn't have applicant id. trying set 1 doesn't make sense.
you're doing right thing already. in block you're correctly passing instance
argument formset instantiation, associate applicant. you're not doing in post block, , should be:
form = applicantform(request.post, instance=applicant) formset = educationinlineformset(request.post, instance=applicant)
Comments
Post a Comment