user12295267
user12295267

Reputation:

How to save all related data using django signals post_save

I just want that if the admin insert the Course(ABM) and Education Level (Grade 11) and Section(Chronicles) it will get all related data in subjectsectionteacher(second picture) and it will automatic save to student enrolled subject(third picture) my problem is only one data save.

enter image description here

subjectsectionteacher enter image description here

this is my code in models.py

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 SubjectSectionTeacher(models.Model):
    School_Year = models.ForeignKey(SchoolYear, related_name='+', on_delete=models.CASCADE, null=True)
    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)
    Subjects = models.ForeignKey(Subject, related_name='+', on_delete=models.CASCADE, null=True)
    Employee_Users = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE, null=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, **kwargs):
        teachers = SubjectSectionTeacher.objects.all().filter(Sections=instance.Section,Education_Levels=instance.Education_Levels)
        if created and teachers.exists():
            StudentsEnrolledSubject.objects.update_or_create(
                # This should be the instance not instance.Student_Users
                Students_Enrollment_Records=instance,
                # The below is also not an instance of SubjectSectionTeacher
                Subject_Section_Teacher=teachers.first())

I hope the title and the picture is enough to understand what im trying to say if not, im sorry!

UPDATE this what i want

enter image description here

Upvotes: 0

Views: 97

Answers (2)

user12324446
user12324446

Reputation:

try this

for each in teachers:
            if created and teachers.exists():
                StudentsEnrolledSubject.objects.create(
                        pk=each.id,
                        Students_Enrollment_Records=instance,
                        Subject_Section_Teacher=each

                    )

Upvotes: 0

Nayan Goenka
Nayan Goenka

Reputation: 115

You can use the save() method on the models to update the related data.

within the model class, you should define a method as

def save(self, *args, **kwargs):
    #Code here
    if self.courses = "ABM":
        self.x = Model2.objects.get(id=x) #Whatever code you want
    super(model_name, self).save(*args, **kwargs)

Upvotes: 1

Related Questions