简体   繁体   English

Django 发出 post_save 更新信号

[英]Django signals post_save update

When the student enrollment record (discount type) is updated the student discount (discount type) is also updated, but what should i do if i want to update the student enrollment record (discount type) using student discount (discount type)学生在校记录(折扣型)更新时,学生折扣(折扣型)也随之更新,但是想用学生折扣(折扣型)更新学生在校记录(折扣型)怎么办?

This is my models.py这是我的模型.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)

if i update the student enrollment record (Discount_Type)如果我更新学生注册记录 (Discount_Type)

在此处输入图像描述

the student discount (discount type) update as well学生折扣(折扣类型)也更新

在此处输入图像描述

but when i update the student discount (discount_type)但是当我更新学生折扣 (discount_type)

在此处输入图像描述

the student enrollment record (discount type) doesn't update学生入学记录(折扣类型)不更新

在此处输入图像描述

You need to write a signal handler for when the studentDiscount instance is created or updated.您需要为创建或更新studentDiscount实例时编写信号处理程序。 You'd need similar methods to studentDiscount.create , but it would need to exist on StudentsEnrollmentRecord and the sender be studentDiscount .您需要与studentDiscount.create类似的方法,但它需要存在于StudentsEnrollmentRecord上并且发件人是studentDiscount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM