Django signals foreignkey

in my model I have this code:

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)
Remarks = models.TextField(max_length=500, null=True, blank=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)

\model.py

def studentenrolled(sender):
if StudentsEnrollmentRecord.Section == SubjectSectionTeacher.Sections:
    StudentsEnrollmentRecord.School_Year = SubjectSectionTeacher.School_Year
    StudentsEnrollmentRecord.Education_Levels = SubjectSectionTeacher.Education_Levels
    StudentsEnrollmentRecord.Courses = SubjectSectionTeacher.Courses
    StudentsEnrollmentRecord.Sections = SubjectSectionTeacher.Sections
    ireceive = StudentsEnrolledSubject.objects.create(Students_Enrollment_Records=StudentsEnrollmentRecord.Student_Users                                                                            ,Subject_Section_Teacher=SubjectSectionTeacher.Employee_Users)
    post_save.connect(studentenrolled, sender=StudentsEnrollmentRecord)

I just want that everytime the admin input/update the StudentsEnrollmentRecord.Section and StudentsEnrollmentRecord.Course, it will automatic search the Employee_Users from SubjectSectionTeacher and it will save automatically in StudentsEnrolledSubject

I dont know if i am doing right in django signal, please help me :frowning:

UPDATE in my model django signal

def studentenrolled(sender, **kwargs):
if kwargs['created']:
    student = StudentsEnrollmentRecord.Student_Users
    teacher = SubjectSectionTeacher.Employee_Users
    ireceived = StudentsEnrolledSubject.objects.create(Students_Enrollment_Records=student
        , Subject_Section_Teacher=teacher)
post_save.connect(studentenrolled, sender=StudentsEnrollmentRecord)

i received this error

ValueError at /admin/Homepage/studentsenrollmentrecord/add/

Cannot assign “<django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x04532450>”: “StudentsEnrolledSubject.Students_Enrollment_Records” must be a “StudentsEnrollmentRecord” instance.

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/Homepage/studentsenrollmentrecord/add/
Django Version: 2.2.4
Exception Type: ValueError
Exception Value: Cannot assign “<django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x04532450>”: “StudentsEnrolledSubject.Students_Enrollment_Records” must be a “StudentsEnrollmentRecord” instance.
Exception Location: C:\Users’\Desktop\Projects\venv\lib\site-packages\django\db\models\fields\related_descriptors.py in set, line 211
Python Executable: C:\Users’\Desktop\Projects\venv\Scripts\python.exe
Python Version: 3.7.4
Python Path: ['C:\Users\‘’ ''\Desktop\SchoolWebsite-Project\SchoolWebsite-Project1’, 'C:\Users\‘’ ''\AppData\Local\Programs\Python\Python37-32\python37.zip’, 'C:\Users\‘’ ''\AppData\Local\Programs\Python\Python37-32\DLLs’, ‘C:\Users\’ '\AppData\Local\Programs\Python\Python37-32\lib’, 'C:\Users\‘\AppData\Local\Programs\Python\Python37-32’, 'C:\Users\‘\Desktop\Projects\venv’, 'C:\Users\‘\Desktop\Projects\venv\lib\site-packages’, 'C:\Users\‘’ ''\AppData\Local\Programs\Python\Python37-32\lib\site-packages’]
Server time: Wed, 30 Oct 2019 03:45:54 +0000

That if statement is wrong i think. What you want might be this.
@receiver(models.signals.pre_save, sender=StudentsEnrollmentRecord)
def studentenrolled(sender, instance, created, **kwargs):
if created:
subjectsectionteacher=SubjectSectionTeacher.objects.get(Sections=instance.Section)
instance.School_Year = subjectsectionteacher.School_Year
… and so on
also post_save.connect() not required in this method. This is a pre_save signal, as post_save signals will have recurring issues which can be solved using signal id.

The below thing is also wrong.
student = StudentsEnrollmentRecord.Student_Users
teacher = SubjectSectionTeacher.Employee_Users
ireceived = StudentsEnrolledSubject.objects.create(Students_Enrollment_Records=student
, Subject_Section_Teacher=teacher)

it shoud be.
student = instance
teacher = subjectsectionteacher
ireceived = StudentsEnrolledSubject.objects.create(Students_Enrollment_Records=student
, Subject_Section_Teacher=teacher)

https://stackoverflow.com/questions/58687439/how-to-get-all-related-data-using-django-signal-post-save-and-save-it-to-another sir thank you so much for answering this question, please try to look this question sir,