[Solved]-How to remove username field in the register form on django admin?

13👍

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

2👍

You need to define your own custom user model to remove it from Django admin. If you can live with Django’s user model with the username, but want it removed from serializers (DRF), you could define your own custom login and register serializers and remove username there.

1👍

In your settings.py file:

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False

https://django-allauth.readthedocs.io/en/latest/advanced.html#custom-user-models

0👍

You can also override the base admin template as described in the SO answers here:
How to override and extend basic Django admin templates?

0👍

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 🙂

👤Jerry

Leave a comment