0
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()方法来根据用户自定义视图它在超级用户登录时工作得很好但问题是当工作人员登录然后超级用户登录时只显示employee_verified我猜问题是现在等于“雇主验证”的字段中没有排除任何内容

4

1 回答 1

1

你不应该self.excludeget_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']
于 2012-05-22T13:29:07.423 回答