django admin 的 多对多过滤

    #多对多的列表过滤
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == 'breakfast' or db_field.name =='lunch' or db_field.name =='dinner':
            kwargs["queryset"] = Food.objects.filter(company=request.user.company)
            from django.contrib.admin import widgets
            kwargs['widget'] = widgets.FilteredSelectMultiple(
                db_field.verbose_name,
                db_field.name in self.filter_vertical
            )
        return super(MenuAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

你可能感兴趣的:(django admin 的 多对多过滤)