-2

I just want that when the admin update the student enrollment record (picture1) and get all the related record in ScheduleOfPayment(picture2) and must be added automatically in StudentsPaymentSchedule(picture3) (remarks, date, amount Payment Schedule from ScheduleOfPayment)

class StudentsPaymentSchedule(models.Model):
    Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',
                                                    on_delete=models.CASCADE, null=True)
    Payment_Schedule = models.DateField(null=True, blank=True)
    Amount = models.FloatField(null=True, blank=True)
    Remarks = models.TextField(max_length=500, 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.Payment_Type != instance.Payment_Type or \
                    older_instance.Education_Levels != instance.Education_Levels:

                StudentsEnrolledSubject.objects.filter(
                    Students_Enrollment_Records=instance
                ).delete()
            else:
                return None
        studentspayment = ScheduleOfPayment.objects.filter(Education_Levels=instance.Education_Levels, Payment_Type=instance.Payment_Type)
        for each in studentspayment:
                StudentsEnrolledSubject.objects.create(
                    Students_Enrollment_Records=instance,
                    Payment_Schedule=each)

class StudentsEnrollmentRecord(models.Model):
    Student_Users = models.ForeignKey(StudentProfile, related_name='students', 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)
    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)
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE,blank=True,null=True)

class ScheduleOfPayment(models.Model):
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=True, null=True)
    Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE,blank=True, null=True)
    Payment_Type = models.ForeignKey(PaymentType, related_name='+', on_delete=models.CASCADE, blank=True, null=True)
    Display_Sequence = models.IntegerField(blank=True, null=True)
    Date = models.DateField(null=True,blank=True)
    Amount = models.FloatField(null=True, blank=True)

picture 1 enter image description here

picture 2 enter image description here

picture 3 enter image description here

New contributor
kaito is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Browse other questions tagged .