Django信号后保存更新

问题描述 投票:0回答:1

[学生入学记录(折扣类型)更新时,学生折扣(折扣类型)也被更新,但是如果我想使用学生折扣(折扣类型)更新学生入学记录(折扣类型)怎么办]] >

这是我的模型。py

class studentDiscount(models.Model):
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',
                                                    on_delete=models.CASCADE, null=True)
    Discount_Type = models.ForeignKey(Discount, related_name='+', on_delete=models.CASCADE, null=True,blank=True)

    @receiver(pre_save, sender=StudentsEnrollmentRecord)
    def get_older_instance(sender, instance, *args, **kwargs):
        try:
            instance._old_instance = StudentsEnrollmentRecord.objects.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:
            older_instance = instance._old_instance
            if older_instance.Discount_Type != instance.Discount_Type:

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

        discount = studentDiscount.objects.filter(Discount_Type=instance.Discount_Type)
        if created:
            print("nagsulod")
            studentDiscount.objects.create(
                Students_Enrollment_Records=instance,
                Discount_Type=instance.Discount_Type)
        else:
            studentDiscount.objects.create(
                Students_Enrollment_Records=instance,
                Discount_Type=instance.Discount_Type)

    def __str__(self):
        suser = '{0.Students_Enrollment_Records}'
        return suser.format(self)

class StudentsEnrollmentRecord(models.Model):
    Student_Users = models.ForeignKey(StudentProfile, on_delete=models.CASCADE,null=True)
    School_Year = models.ForeignKey(SchoolYear, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    strands = models.ForeignKey(strand, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    Section = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True,blank=True)
    Payment_Type = models.ForeignKey(PaymentType, related_name='+', on_delete=models.CASCADE, null=True)
    Discount_Type = models.ForeignKey(Discount, related_name='+', on_delete=models.CASCADE, null=True,blank=True)
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE,blank=True,null=True)
    ESC = models.ForeignKey(esc, on_delete=models.CASCADE,null=True,blank=True)
    Remarks = models.TextField(max_length=500,null=True,blank=True)

class Discount(models.Model):
    Code=models.CharField(max_length=500,blank=True)
    Discount_Type=models.CharField(max_length=500,blank=True)
    Remarks=models.TextField(max_length=500,blank=True)
    TuitionFee_Discount_Amount=models.FloatField(null=True,blank=True)
    TuitionFee_Discount_Rate = models.FloatField(null=True,blank=True)
    Miscellaneous_Discount_Amount=models.FloatField(null=True,blank=True)
    Miscellaneous_Discount_Rate = models.FloatField(null=True,blank=True)

如果我更新学生入学记录(折扣类型)

enter image description here

学生折扣(折扣类型)也更新

enter image description here

但是当我更新学生折扣(折扣类型)

enter image description here

学生注册记录(折扣类型)未更新

enter image description here

[更新学生登记记录(折扣类型)时,学生折扣(折扣类型)也会更新,但是如果我想更新学生登记记录(折扣...),该怎么办?”>

< [
您需要为创建或更新studentDiscount实例时编写信号处理程序。您需要与studentDiscount.create类似的方法,但它需要在StudentsEnrollmentRecord上存在,发送方为studentDiscount
django django-models django-signals
1个回答
0
投票
您需要为创建或更新studentDiscount实例时编写信号处理程序。您需要与studentDiscount.create类似的方法,但它需要在StudentsEnrollmentRecord上存在,发送方为studentDiscount
© www.soinside.com 2019 - 2024. All rights reserved.