I have this table StudentProfile
and table StudentEnrollmentRecord, I just want to add an column before the Student User just like the picture below,
this is my code in model.py
class StudentProfile(models.Model):
LRN = models.CharField(max_length=500,null=True)
Firstname = models.CharField(max_length=500,null=True,blank=True)
Middle_Initial = models.CharField(max_length=500,null=True,blank=True)
Lastname = models.CharField(max_length=500,null=True,blank=True)
class StudentsEnrollmentRecord(models.Model):
Student_Users = models.ForeignKey(StudentProfile, related_name='+', 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)
and this is my code in admin.py
class StudentsEnrollmentRecordAdmin(admin.ModelAdmin):
list_display = ('Student_Users', 'School_Year', 'Courses', 'Section', 'Payment_Type', 'Education_Levels')
ordering = ('Education_Levels',)
list_filter = ('Education_Levels','Section','Student_Users')
You will need to define a separate function in your Admin class to retrieve the column from your related object:
class StudentsEnrollmentRecordAdmin(admin.ModelAdmin):
...
def lrn(self, obj):
return obj.Student_Users.lrn
and then use it in list_display
list_display = ('lrn', ...)
I also recommend using list_select_related
so it doesn't query DB for every row.
class StudentsEnrollmentRecordAdmin(admin.ModelAdmin):
...
list_select_related = ('Student_Users')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.