How to remove username field in the register form on django admin?

Viewed 8696

using the rest-framework and the django-allauth settings flags for removing the username as required for both login and register:

#settings.py
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False

but i still see the username field in the registration form in the administrator and the username field in the user list (empty users those without username).

how can i remove it?

5 Answers

Essentially this involves creating a custom user model and informing Django and Django Admin portions that it should be used instead of the standard model.

Your class will look like the following.

class User(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

The details of the implementation can be found here

In my case, I simply put 'username = None' there. Then it's gone. Full code:

from django.utils.translation import gettext_lazy as _
class LectureUser(AbstractUser):
  username = None
  identifier = models.IntegerField(primary_key=True)
  email = models.EmailField(_('email address'))
  USERNAME_FIELD = 'identifier' # Set to the unique identifier we define.
  objects = CustomUserManager()

Hope this helps :-)

Related