def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
self.exclude=[]
else:
self.fields=['employer_verified']
return super(EmployerAdmin, self).get_form(request, obj, **kwargs)
我有这个代码来覆盖get_form()方法,以根据用户自定义视图 当超级用户登录但问题是当工作人员登录然后超级用户登录时才显示employees_verified,我的问题就是 没有任何东西被排除在现在与“雇主验证”相等的领域之外
答案 0 :(得分:1)
您不应在self.exclude
方法中设置get_form
,它不是线程安全的。相反,您可以定义两个不同的表单类,并根据user.is_superuser
返回正确的表单类。
class EmployerForm(forms.ModelForm):
class Meta:
model = Employer
fields = ['employer_verified',]
class EmployerSuperUserForm(forms.ModelForm):
class Meta:
model = Employer
def get_form(self, request, obj=None, **kwargs):
if request.user.is_superuser:
return EmployerSuperUserForm
else:
return EmployerForm
self.fields=['employer_verified']