动态排除inline django管理员中的字段 [英] Dynamically excluding fields in inline django admin
本文介绍了动态排除inline django管理员中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以用get_formsets,add_view这样的方法来处理这个字段, ,'change_view',但我不知道语法是什么。
任何建议?
解决方案
我使用我的内联类中的下一个代码实现了我需要的功能:
def get_formset self,request,obj = None,** kwargs):
if request.user.groups.all()。count()> 0:
如果request.user.groups.all()[0] .name =='用户组名':
kwargs ['exclude'] = ['field_to_exclude',]
return super(MyInline,self).get_formset(request,obj,** kwargs)
这个问题给了我一些提示:添加和更改页面的不同字段在管理员
I want to exclude some fields in my inline based on my request user.
I know somehow I can handle this with methods like 'get_formsets', 'add_view', 'change_view', but I'm not sure what the syntax is.
Any suggestions?
解决方案
I achieved what I needed with the next code in my inline class:
def get_formset(self, request, obj=None, **kwargs):
if request.user.groups.all().count() > 0:
if request.user.groups.all()[0].name == 'User Group Name':
kwargs['exclude'] = ['field_to_exclude',]
return super(MyInline, self).get_formset(request, obj, **kwargs)
The answer to this question gave me the hints: different fields for add and change pages in admin
这篇关于动态排除inline django管理员中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文