10

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?

DezMaeth
  • 158
  • 1
  • 1
  • 9

5 Answers5

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

Duncan Hoggan
  • 5,082
  • 3
  • 23
  • 29
Maulik Harkhani
  • 390
  • 3
  • 12
  • 2
    Please note that this answer is not complete. The implementation of a `CustomUserManager` is missing which is presented in the article mentioned in the answer https://www.fomfus.com/articles/how-to-use-email-as-username-for-django-authentication-removing-the-username/. Can you please edit your answer to include that? – Fed Apr 09 '21 at 11:34
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.

RobChooses
  • 428
  • 3
  • 12
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

mecampbellsoup
  • 1,260
  • 18
  • 16
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?

Community
  • 1
  • 1
RobChooses
  • 428
  • 3
  • 12
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
  • 11
  • 4