Django TabularInline Admin: put instance value in

2019-08-28 20:07发布

问题:

I wanna get student study class name and match with the subject then filtering her subject within my TabularInline Admin section. I did try to match StdSubject with model admin student instance. I know whole problem in here

kwargs["queryset"] = StdSubject.objects.filter(std_subject_class__std_class_name_N__exact=self.std_name)

and problem with self.std_name in that line I want to match with std_name. But showed error. This is my code.

class MarksSubjectInstanceInline(admin.TabularInline):
    model = Marks

    fk_name = 'std_name'

    extra = 2
    exclude = ['subject_gradepoint', 'subject_gpa','subject_gpa_sub', 'subject_marks', 'subject_total_marks']

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "subject_name":
            kwargs["queryset"] = StdSubject.objects.filter(std_subject_class__std_class_name_N__exact=self.std_name)
        return super().formfield_for_foreignkey(db_field, request, **kwargs)



@admin.register(StudentInfo)
class StudentAdmin(admin.ModelAdmin):

    search_fields = ('std_name','std_roll','std_group')
    list_filter = ('std_class', 'std_gender', 'std_group',)
    list_display = ('std_name', 'std_class', 'std_group', 'std_gender', 'std_roll')
    inlines = [MarksSubjectInstanceInline]

    exclude = ['std_total_marks', 'std_gpa','std_grade_point_total_sum','std_marks_with_fail_sub', 'std_grade_point_total_subject_avg', 'std_fail_subject','school_rank','class_rank']