django-allauth set username the same as email

后端 未结 2 997
死守一世寂寞
死守一世寂寞 2021-01-12 04:01

I have a sign up form which asks only for email and password. When a user signs up, django-allauth creates a username for that user by striping the \"@email\" suffix form th

相关标签:
2条回答
  • 2021-01-12 04:19

    profiles.models.py (custom user model)

    from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
    
    class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
    
        def populate_user(self, request, sociallogin, data):
            user = super().populate_user(request, sociallogin, data)
            user.username = user.email
            return user
    

    settings.py

    SOCIALACCOUNT_ADAPTER = "profiles.models.CustomSocialAccountAdapter"
    
    0 讨论(0)
  • 2021-01-12 04:41

    I do exactly what you want to do with a signal on User pre_save.

    Your settings look ok, so if you add the following code in somewhere like for example core.models.py it will work as you need:

    @receiver(pre_save, sender=User)
    def update_username_from_email(sender, instance, **kwargs):
        user_email = instance.email
        username = user_email[:30]
        n = 1
        while User.objects.exclude(pk=instance.pk).filter(username=username).exists():
            n += 1
            username = user_email[:(29 - len(str(n)))] + '-' + str(n)
        instance.username = username
    

    The reason I do it with a signal is that I want that every time User is saved, the username is updated. You could check if the e-mail has changed update the username only in that case.

    Then I limit the username to the first 30 characters of email (default max lenght of username is 30 chars):

    username = user_email[:30]
    

    You could also change the max lenght of username, but in my case I prefered to use the default lenght.

    Since I make this, it could happen that there are repeated usernames. To avoid repeated usernames, in case that the resulting username after limiting it to 30 characters already exists, I put -2, -3... at the end to make the username unique:

    n = 1
    while User.objects.exclude(pk=instance.pk).filter(username=username).exists():
        n += 1
        username = user_email[:(29 - len(str(n)))] + '-' + str(n)
    instance.username = username
    

    I hope this solution helps you!

    0 讨论(0)
提交回复
热议问题