Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在django post\u save上使用update\u或\u create?_Django - Fatal编程技术网

如何在django post\u save上使用update\u或\u create?

如何在django post\u save上使用update\u或\u create?,django,Django,插入数据工作正常,但当我更新现有数据时,它会创建另一条记录,而不是更新 我尝试的是@ruddra先生的解决方案 class StudentsEnrolledSubject(models.Model): Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',

插入数据工作正常,但当我更新现有数据时,它会创建另一条记录,而不是更新

我尝试的是@ruddra先生的解决方案

class StudentsEnrolledSubject(models.Model):
        Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',
                                                        on_delete=models.CASCADE, null=True)
        Subject_Section_Teacher = models.ForeignKey(SubjectSectionTeacher, related_name='+', on_delete=models.CASCADE,
                                                    null=True,blank=True)      

    @receiver(post_save, sender=StudentsEnrollmentRecord)
            def create(sender, instance, created, *args, **kwargs):
                teachers = SubjectSectionTeacher.objects.filter(Sections=instance.Section,
                                                                Education_Levels=instance.Education_Levels,
                                                                Courses=instance.Courses)
            for each in teachers:
                if created or teachers.exists():
                    print("if")
                    StudentsEnrolledSubject.objects.create_or_update(
                        pk=each.id,
                        Students_Enrollment_Records=instance,
                        Subject_Section_Teacher=each

                    )
它起作用了!但当我试图保存另一条包含多个相关数据的记录时,它只保存了一条记录。

在您的代码中:

            StudentsEnrolledSubject.objects.update_or_create(
                Students_Enrollment_Records=instance,
                defaults= {'Subject_Section_Teacher': each}

            )

更新: 我认为你不应该使用
update\u或\u create
信号。如果您使用
update\u或\u create
,当您更改
StudentsEnrollmentRecord
的课程或教育水平等时,教师人数也将不相同

要获取旧实例,请使用
pre_save
信号:

StudentsEnrolledSubject.objects.update_or_create(
    Students_Enrollment_Records=instance,
    defaults= {'Subject_Section_Teacher':each}
)

在这里,我删除了所有
学生注册主题
,如果
课程
部分
教育水平
中的任何值与旧实例不同。因为正如我前面所说的,教师的数量将不一样。因此,我删除了这些实例,并与新教师创建了新实例。

应该是
update\u或\u create
:)请查看更新的答案,先生,但是当我保存时,我的数据库中只保存了一条记录,当我删除pk=each.id时,只保存了一条记录,请查看更新部分,最好谨慎使用。请确保在使用数据库之前对其进行备份。因为您可能会丢失数据I get此错误sir无法从此行“old_instance=StudentsEnrollmentRecord.objects.get(instance.pk)”解包不可iterable的非类型对象
StudentsEnrolledSubject.objects.update_or_create(
    Students_Enrollment_Records=instance,
    defaults= {'Subject_Section_Teacher':each}
)
class StudentsEnrollmentRecord(models.Model):
    # ...

    @receiver(pre_save, sender=StudentsEnrollmentRecord)
    def get_older_instance(sender, instance, *args, **kwargs):
        try:
           instance._old_instance = StudentsEnrollmentRecord.object.get(pk=instance.pk)
        except StudentsEnrollmentRecord.DoesNotExist:
           instance._old_instance = None

    @receiver(post_save, sender=StudentsEnrollmentRecord)
    def create(sender, instance, created, *args, **kwargs):
        if not created:
           old_instance = instance._old_instance
           if older_instance.Courses != instance.Courses or \
              older_instance.Sections != instance.Sections or \
              older_instance.Education_Levels != instance.Education_Levels:

                 StudentsEnrolledSubject.objects.filter(
                   Students_Enrollment_Records=instance
                 ).delete()
           else:
               return None


       teachers = SubjectSectionTeacher.objects.filter(Sections=instance.Section,  Education_Levels=instance.Education_Levels, Courses=instance.Courses)
       for each in teachers:
             StudentsEnrolledSubject.objects.create(
                Students_Enrollment_Records_id=instance.pk,
                Subject_Section_Teacher=each
             )