我只希望管理员插入“课程”(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='+',null=True,blank=True)
Courses = models.ForeignKey(Course,blank=True)
Section = models.ForeignKey(Section,blank=True)
Payment_Type = models.ForeignKey(PaymentType,null=True)
Education_Levels = models.ForeignKey(EducationLevel,blank=True,null=True)
class SubjectSectionTeacher(models.Model):
School_Year = models.ForeignKey(SchoolYear,blank=True)
Sections = models.ForeignKey(Section,null=True)
Subjects = models.ForeignKey(Subject,null=True)
Employee_Users = models.ForeignKey(EmployeeUser,null=True)
class StudentsEnrolledSubject(models.Model):
Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord,null=True)
Subject_Section_Teacher = models.ForeignKey(SubjectSectionTeacher,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','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','Sections','Subjects','Employee_Users','Status')
ordering = ('School_Year',)
def lrn(self,obj):
return obj.Student_Users.lrn