Django TabularInline Admin:将实例值放入formfield_for_foreignkey

时间:2018-09-21 08:21:05

标签: django django-models django-forms django-admin django-admin-filters

我想获得学生学习的班级名称并与该主题匹配,然后在我的TabularInline Admin部分中过滤她的主题。我确实尝试将StdSubject与模型管理学生实例匹配。 我在这里知道整个问题

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

以及该行中的self.std_name问题,我想与std_name匹配。但是显示错误。 这是我的代码。

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']

0 个答案:

没有答案