class MachineAdmin(PermissionMixin, admin.ModelAdmin):
form = MachineForm
....
def get_form(self, request, obj=None, change=False, **kwargs):
form = super().get_form(request, obj, change, **kwargs)
# 在form里没法过滤,因为form对象没有request,因此,在get_form方法里过滤后,给form对象设置进去
form.base_fields['binds'].queryset = QQGroup.objects.filter(user=request.user)
return form
此方法也适用于过滤autocomplete_fields