[Answer]-Overiding get_form() method to customize admin view according to user

1👍

You shouldn’t set self.exclude in the get_form method, it’s not thread safe. Instead, you can define two different form classes, and return the correct one depending on 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']

Leave a comment