class StudentsTeacher(admin.TabularInline):
form = StudentsTeacher
model = StudentsTeacher
class Teacher_Admin(admin.ModelAdmin):
inlines = (StudentsTeacher,)
我想在 StudentTeacher select 中添加一个带有过滤器的查询集,但它不起作用,内联总是显示所有。
使用该get_queryset()
方法,您可以覆盖查询集
class Teacher_Admin(admin.TabularInline):
def get_queryset(self, request):
qs = super(Teacher_Admin, self).get_queryset(request)
return qs.filter(<filtering>)
您可以在django 文档中了解更多信息
您还可以在表单集中为内联定义自定义查询集
class StudentTeacherFormset(BaseInlineFormSet):
self.queryset = StudentsTeacher.objects.filter(<your custom filter>)
class StudentsTeacher(admin.TabularInline):
form = StudentsTeacher
model = StudentsTeacher
formset = StudentTeacherFormset
class Teacher_Admin(admin.ModelAdmin):
inlines = (StudentsTeacher,)
Formset 可用于为 django 内联生成自定义查询集,而不是在 parent admin 中呈现子表的所有对象。
但是,如果有人找到在 django 内联中提供类似于列表过滤器的过滤器的解决方案,请告诉我。