我可以使用filter_horizontal工作,但是只要我添加一个formfield_for_manytomany它就会停止工作。有没有办法使用formfield_for_manytomany并维护filter_horizontal?
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)
答案 0 :(得分:2)
formfield_for_manytomany
和filter_horizontal
应该可以很好地协同工作。
问题是当你拨打super()
时,你正在调用错误的方法。它应该是
return super(MyModelAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)