William Le
William Le

Reputation: 1179

Remove username field from Django Allauth

I'm trying to remove the username field after overriding the Django Allauth package. I tried the following suggestions:

  1. Set ACCOUNT_USER_MODEL_USERNAME_FIELD = None and some more inside settings.py as following:
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_PASSWORD_MIN_LENGTH = 8
  1. Overriding User Model in venv\Lib\site-packages\django\contrib\auth\models.py
class User(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)
    USERNAME_FIELD = 'email'
    class Meta(AbstractUser.Meta):
        swappable = "AUTH_USER_MODEL"

both failed, as when I run py manage.py createsuperuser, I still got the username field popping up:

Username (leave blank to use 'admin'):

How can I safely remove the username field while overriding Django Allauth?

Could you show me a way to solve this?

Thank you!

Upvotes: 0

Views: 940

Answers (1)

Ridwan
Ridwan

Reputation: 111

Both django and django-allauth has have the username and email field. And by you defining a CutomUser means you're trying to cutomize the default user model you don't have to re-declare the variables again. Here is how your new models.py will look like

from django.db import models
from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    pass

Then update your settings.py with:

ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False

ACCOUNT_USERNAME_REQUIRED = False    # This removes the username field
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True

Upvotes: 1

Related Questions