Ev.
Ev.

Reputation: 1567

How save email equals username Allauth Social Media Account Django

I managed to setup allauth to work on my project and use the social media login. However I was wondering if they had any option to set the e-mail fetched Facebook to the username. I read their documentation but I couldn't see any of that in the variables.

I found this link, but they change Django's core files. I was hoping to find something more suitable.

My current configuration:

ACCOUNT_USER_MODEL_EMAIL_FIELD = 'email'
ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_EMAIL_REQUIRED = True

Also, is it possible to use only the social media login/creation creation from Allauth? I can see it is possible to create an account if you access localhost/accounts/create/ (or a similar url). I don't need that since I have my own account creation page.

Upvotes: 1

Views: 890

Answers (1)

Ev.
Ev.

Reputation: 1567

Well, you don't need to change Django files. Just add this to your models.py:

from django.db.models.signals import pre_save

@receiver(pre_save, sender=User)
def update_username_from_email(sender, instance, **kwargs):
    instance.username = instance.email

Assuming an e-mail has been given, it will set the e-mail as the username every time a new user is about to be saved to the database.

Upvotes: 5

Related Questions