Search code examples
pythondjangoformset

save() prohibited to prevent data loss due to unsaved related object 'employee'


I am working on inlineformset_factory. Inlineformset_factory has two model(WorkExperience and Education) related with Employee model using foreign key relation. When I submit the form I am getting a value error. The error is:

save() prohibited to prevent data loss due to unsaved related object 'employee'

This is my post method:

def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        # work_form = self.work_form_class(request.POST, prefix='work_form')
        # education_form = self.education_form_class(request.POST, prefix='education_form')
        work_formset = self.work_formset_class(request.POST or None, request.FILES, prefix='work_form')
        education_formset = self.education_formset_class(request.POST or None, request.FILES, prefix='education_form')
        data = request.POST.copy()

        # Check form validation
        if form.is_valid() and work_formset.is_valid() and education_formset.is_valid():
            instance = form.save()
            # Save work experience
            for work_form in work_formset:
                work = work_form.save(commit=False)
                work.employee_id = instance.id
                work.save()
            work_formset.save()
            # Save education experience
            for education_form in education_formset:
                education = education_form.save(commit=False)
                education.employee_id = instance.id
                education.save()
            education_formset.save()

models.py:

class Employee(models.Model):
    """
    Create employee attributes
    """

    employee_user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True)
    e_id = models.IntegerField(unique=True, null=True)
    first_name = models.CharField(max_length=128, null=True)
    last_name = models.CharField(max_length=128, null=True)

class WorkExperience(models.Model):
    """
    Stores employee previous work experiences
    """
    employee = models.ForeignKey('Employee', related_name='work_experience', on_delete=models.CASCADE, null=True)
    previous_company_name = models.CharField(max_length=128, null=True)
    job_designation = models.CharField(max_length=128, null=True)
    from_date = models.DateField(null=True)
    to_date = models.DateField(null=True)
    job_description = models.CharField(max_length=256, null=True)


class Education(models.Model):
    """
    Stores employee education background
    """
    employee = models.ForeignKey('Employee', related_name='education', on_delete=models.CASCADE, null=True)
    institution_name = models.CharField(max_length=128, null=True)
    degree = models.CharField(max_length=128, null=True)
    passing_year = models.IntegerField(null=True)
    result = models.DecimalField(max_digits=5, decimal_places=2, null=True)

I dont know where the error. Where is the error that is preventing to save()?


Solution

  • Problem solved!

    added instance=form.instance

    work_formset = self.work_formset_class(request.POST or None, request.FILES, instance=form.instance, prefix='work_form')
    education_formset = self.education_formset_class(request.POST or None, request.FILES, instance=form.instance, prefix='education_form')