filter_horizo​​ntal在使用formfield_for_manytomany时不起作用

时间:2015-12-10 20:18:35

标签: django django-admin

我可以使用filter_horizo​​ntal工作,但是只要我添加一个formfield_for_manytomany它就会停止工作。有没有办法使用formfield_for_manytomany并维护filter_horizo​​ntal?

class MyModelAdmin(admin.ModelAdmin):

    filter_horizontal = ('users',)

    def formfield_for_manytomany(self, db_field, request=None, **kwargs):
        if db_field.name == 'users':
            kwargs["queryset"] = User.objects.filter(profile__api_user=True)

        return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

1 个答案:

答案 0 :(得分:2)

formfield_for_manytomanyfilter_horizontal应该可以很好地协同工作。

问题是当你拨打super()时,你正在调用错误的方法。它应该是

return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)