Using Django Allauth without Username Field
Learn how to use Django Allauth without a username field in your User model.
Published: 01 Sep, 2024
Earlier, I showed you how to remove the username field from Django’s User model.
If you also use Django Allauth for your authentication logic, you’ll need to configure a few things before it starts working because it expects a username by default.
See this Github project for sample code.
After having setup Allauth, add the following to settings.py:
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = "email"
These will disable username related functionality.
Now create a function that Allauth can use to get the user’s display name:
def get_user_display(user):
return user.email
Then, tell Allauth to use it in settings.py:
...
ACCOUNT_USER_DISPLAY = "yourapp.utils.get_user_display"
...
Replace yourapp with the name of the app that contains the module with the function.
Try logging in and signing up now and you’ll see that Allauth views work perfectly. Authentication with social accounts should also work.