我只希望管理员插入“课程”(ABM)和“教育水平”(11年级)和“科目”(编年史),它将在subjectsectionteacher(第二张图片)中获取所有相关数据,并将其自动保存到学生注册的学科(第三张图片)我的问题是只有一个数据保存。
这是我在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())
我希望标题和图片足以理解我想说的话,对不起!
更新 这就是我想要的
admin.py
class StudentsEnrollmentRecordAdmin(admin.ModelAdmin):
#inlines = [InLineSubject]
list_display = ('lrn', 'Student_Users', 'Education_Levels', 'Courses', 'Section', 'Payment_Type' ,'School_Year')
#list_select_related = ('Student_Users')
ordering = ('Education_Levels','Student_Users__lrn')
list_filter = ('Student_Users','Education_Levels','Section','Payment_Type')
@admin.register(StudentsEnrolledSubject)
class StudentsEnrolledSubject(admin.ModelAdmin):
list_display = ('Students_Enrollment_Records','Subject_Section_Teacher',)
ordering = ('Students_Enrollment_Records',)
@admin.register(SubjectSectionTeacher)
class SubjectSectionTeacher(admin.ModelAdmin):
list_display = ('School_Year','Education_Levels','Courses','Sections','Subjects','Employee_Users','Education_Levels','Status')
ordering = ('School_Year',)
def lrn(self, obj):
return obj.Student_Users.lrn