0
class SubjectSectionTeacher(models.Model):
    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)




class StudentsEnrollmentRecord(models.Model):
    Student_Users = models.ForeignKey(StudentProfile, related_name='students', on_delete=models.CASCADE, null=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)
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE, blank=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, *args, **kwargs):
                teachers = SubjectSectionTeacher.objects.filter(Sections=instance.Section,
                                                                Education_Levels=instance.Education_Levels,
                                                                Courses=instance.Courses)
            for each in teachers:
                if created or teachers.exists():
                    print("if")
                    StudentsEnrolledSubject.objects.create(
                        pk=each.id,
                        Students_Enrollment_Records=instance,
                        Subject_Section_Teacher=each

                    )

Inserting data is working perfectly but when I update the existing data it creates another record not update

note: I already tried this StudentsEnrolledSubject.objects.create and StudentsEnrolledSubject.objects.update_or_create, but it still the same result

radoh
  • 4,554
  • 5
  • 30
  • 45

2 Answers2

0

The problem is in your if statement and the way you create new instances. try changing the code as the follow:

 for each in teachers:
     StudentsEnrolledSubject.objects.create_or_update(
            Students_Enrollment_Records=instance,
            Subject_Section_Teacher=each
     )
Mohsen Mahmoodi
  • 331
  • 2
  • 8
  • And why are you assigning the `pk` and `Subject_Section_Teacher` at the same time with the same value?? Can you post your models structure? – Mohsen Mahmoodi Nov 07 '19 at 02:47
  • I edit my question sir, please help me with my problem. –  Nov 07 '19 at 02:56
  • The problem is in your if statement's logic. ` if created or teachers.exists():` You are checking if the the record is created for the first time or if there is an old `StudentsEnrolledSubject` exists.I will change my answer accordingly – Mohsen Mahmoodi Nov 07 '19 at 03:13
  • There's a bit more problems that just the `if` in OPs question – radoh Nov 27 '19 at 08:23
0

Condition

for each in teachers:
    if created or teachers.exists():

is wrong, teachers.exists() will be always true, since you already iterate through teachers.

Another problem is, that signal functions shouldn't be an instance/class method - move it outside of your model. Currently the sender parameter would actually be self and so on, so created would be wrong as well. (or give it a @staticmethod decorator)

Since you want to create the record only during StudentsEnrollmentRecord creation, you should handle the created parameter at the beginning of the signal function - this also saves you some queries.

Also, give your signal function a more meaningful name, maybe generate_subjects?

So the signal could look like this

@receiver(post_save, sender=StudentsEnrollmentRecord)
def create(sender, instance, created, *args, **kwargs):
    if not created:
        return
    teachers = SubjectSectionTeacher.objects.filter(...)
    for each in teachers:
        StudentsEnrolledSubject.objects.create(...)


class StudentsEnrolledSubject(models.Model):
   ...
radoh
  • 4,554
  • 5
  • 30
  • 45