django post_save: update_or_create 不工作
django post_save: update_or_create not working
class SubjectSectionTeacher(models.Model):
Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True)
Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Sections = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True)
class StudentsEnrollmentRecord(models.Model):
Student_Users = models.ForeignKey(StudentProfile, related_name='students', on_delete=models.CASCADE, null=True)
Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Section = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True,
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(
pk=each.id,
Students_Enrollment_Records=instance,
Subject_Section_Teacher=each
)
插入数据工作正常,但是当我更新现有数据时,它会创建另一条记录而不是更新
注意:我已经尝试过 StudentsEnrolledSubject.objects.create 和 StudentsEnrolledSubject.objects.update_or_create,但结果仍然相同
问题出在您的 if
语句和您创建新实例的方式上。尝试更改代码如下:
for each in teachers:
StudentsEnrolledSubject.objects.create_or_update(
Students_Enrollment_Records=instance,
Subject_Section_Teacher=each
)
条件
for each in teachers:
if created or teachers.exists():
是错误的,teachers.exists()
将 始终为真,因为您已经遍历了 teachers
.
另一个问题是,signal functions shouldn't be an instance/class method - 将它移出模型。目前 sender
参数实际上是 self
等等,所以 created
也是错误的。 (或者给它一个 @staticmethod
装饰器)
由于您只想在 StudentsEnrollmentRecord
创建期间创建记录,因此您应该在信号函数的开头处理 created
参数 - 这也为您节省了一些查询。
另外,给你的信号函数一个更有意义的名字,也许generate_subjects
?
所以信号可能看起来像这样
@receiver(post_save, sender=StudentsEnrollmentRecord)
def create(sender, instance, created, *args, **kwargs):
if not created:
return
teachers = SubjectSectionTeacher.objects.filter(...)
for each in teachers:
StudentsEnrolledSubject.objects.create(...)
class StudentsEnrolledSubject(models.Model):
...
class SubjectSectionTeacher(models.Model):
Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True)
Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Sections = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True)
class StudentsEnrollmentRecord(models.Model):
Student_Users = models.ForeignKey(StudentProfile, related_name='students', on_delete=models.CASCADE, null=True)
Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Section = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True,
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(
pk=each.id,
Students_Enrollment_Records=instance,
Subject_Section_Teacher=each
)
插入数据工作正常,但是当我更新现有数据时,它会创建另一条记录而不是更新
注意:我已经尝试过 StudentsEnrolledSubject.objects.create 和 StudentsEnrolledSubject.objects.update_or_create,但结果仍然相同
问题出在您的 if
语句和您创建新实例的方式上。尝试更改代码如下:
for each in teachers:
StudentsEnrolledSubject.objects.create_or_update(
Students_Enrollment_Records=instance,
Subject_Section_Teacher=each
)
条件
for each in teachers:
if created or teachers.exists():
是错误的,teachers.exists()
将 始终为真,因为您已经遍历了 teachers
.
另一个问题是,signal functions shouldn't be an instance/class method - 将它移出模型。目前 sender
参数实际上是 self
等等,所以 created
也是错误的。 (或者给它一个 @staticmethod
装饰器)
由于您只想在 StudentsEnrollmentRecord
创建期间创建记录,因此您应该在信号函数的开头处理 created
参数 - 这也为您节省了一些查询。
另外,给你的信号函数一个更有意义的名字,也许generate_subjects
?
所以信号可能看起来像这样
@receiver(post_save, sender=StudentsEnrollmentRecord)
def create(sender, instance, created, *args, **kwargs):
if not created:
return
teachers = SubjectSectionTeacher.objects.filter(...)
for each in teachers:
StudentsEnrolledSubject.objects.create(...)
class StudentsEnrolledSubject(models.Model):
...