【问题标题】:save() prohibited to prevent data loss due to unsaved related object 'employee'禁止保存()以防止由于未保存的相关对象“员工”而导致数据丢失
【发布时间】:2020-09-30 15:56:02
【问题描述】:

我正在研究 inlineformset_factory。 Inlineformset_factory 有两个模型(WorkExperience 和 Education)与使用外键关系的 Employee 模型相关。当我提交表单时,我收到一个值错误。 错误是:

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

这是我的发帖方式:

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)

我不知道错误在哪里。阻止 save() 的错误在哪里?

【问题讨论】:

  • 你能发布你的模型吗?看看他们之间的关系如何
  • 我已经编辑了我的问题

标签: python django formset


【解决方案1】:

问题解决了!

添加了 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')

【讨论】:

    猜你喜欢
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多