0

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

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)

enter image description here

the student discount (discount type) update as well

enter image description here

but when i update the student discount (discount_type)

enter image description here

the student enrollment record (discount type) doesn't update

enter image description here

1 Answers1

0

You need to write a signal handler for when the studentDiscount instance is created or updated. You'd need similar methods to studentDiscount.create, but it would need to exist on StudentsEnrollmentRecord and the sender be studentDiscount.

schillingt
  • 13,493
  • 2
  • 32
  • 34
  • Then what should I do mr @schillingt? Can you provide example using my code? –  Jan 24 '20 at 17:06
  • 1
    No, I think this is a good learning opportunity for you. Your code handles the one direction, now you need to reverse it for the other. I've provided the direction, the implementation is left to you. – schillingt Jan 24 '20 at 17:10