[Answer]-Django admin not working with custom user

1👍

is_admin is not something that django’s authentication system knows about. In the authentication form that is used, it is only checking if the user is active or is staff:

class AdminAuthenticationForm(AuthenticationForm):
    """
    A custom authentication form used in the admin app.
    """
    error_messages = {
        'invalid_login': _("Please enter the correct %(username)s and password "
                           "for a staff account. Note that both fields may be "
                           "case-sensitive."),
    }
    required_css_class = 'required'

    def confirm_login_allowed(self, user):
        if not user.is_active or not user.is_staff:
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name}
            )

In the original user model, is_staff is a model field. You do not have such a field, but rather a method. This could be a why its not working.

You can solve this problem two ways:

  1. Create your own AdminAuthenticationForm and adjust the confirm_login_allowed method to check for is_admin rather than is_staff.

  2. Create a is_staff property in your custom user model:

    @property
    def is_staff(self):
        return self._is_admin
    

Leave a comment