django allauth user_signed_up信号不起作用

时间:2018-07-21 09:47:59

标签: python django django-allauth

from django.db import models
import time
from django.contrib.auth.models import User
from allauth.account.signals import user_signed_up, password_set
from django.db.models.signals import post_save
from django.dispatch import receiver, Signal

@receiver(user_signed_up)
def employeeID(sender, **kwargs):
    # username = kwargs['request'].user.username
    username = kwargs['user'].username
    u = User.objects.get(username = username)
    u.username = str(time.time()).split('.')[0]
    u.save()

# Create your models here.
# @receiver(user_signed_up)
# def employeeID(sender=User, **kwargs):
#     old_username = kwargs['username']
#     user = User.objects.get(username = old_username)
#     user.username = str(time.time()).split('.')[0]
#     user.save()

# @receiver(user_signed_up)
# # def employeeID(request, user, sociallogin=None, **kwargs):
# def employeeID(sender, **kwargs):
#     print('signal received')
#     old_username = kwargs.username
#     user = User.objects.get(username = old_username)
#     user.username = str(time.time()).split('.')[0]
#     user.save()

# @receiver(user_signed_up)
# def employeeID(sender, **kwargs):
#     request = kwargs['request']
#     user = kwargs['user']
#     # Do your stuff with the user
#     username = request.user.username
#     u = User.objects.get(username = username)
#     u.username = str(time.time()).split('.')[0]
#     u.save()

# @receiver(user_signed_up)
# def employeeID(sender=User, **kwargs):
#     request = kwargs['request']
#     user = kwargs['user']
#     # Do your stuff with the user
#     username = request.user.username
#     u = User.objects.get(username = username)
#     u.username = str(time.time()).split('.')[0]
#     u.save()

# @receiver(user_signed_up, sender=User)
# def employeeID(sender=User, **kwargs):
#     username = kwargs['request'].user.username
#     # username = kwargs['user'].username
#     print(username)
#     u = User.objects.get(username = username)
#     u.username = str(time.time()).split('.')[0]
#     u.save()

# @receiver(user_signed_up)
# def employeeID(request, user, **kwargs):
#     # username = kwargs['request'].user.username
#     username = kwargs['user'].username
#     print(username)
#     u = User.objects.get(username = username)
#     u.username = str(time.time()).split('.')[0]
#     u.save()

Django版本:2.0

Python 3.6.2

*使用电子邮件注册用户(未经验证)和密码

目标:注册后用我自己的权利更改django-allauth的自动生成的用户名。

问题:我不确定信号是否正常工作,或者我的代码有问题,但是我无法使用自己的名称更改自动生成的django allauth用户名。 我还在这里发布了尝试的各种迭代(注释)。这些只是最近几天我的代码的众多迭代和尝试中的极少数。到目前为止,我已经花费了整个星期五和星期六的时间来实现这一目标。 我已经引用了以下资源,并试图模仿或使它们适应我的用例,但没有成功。

Django - User and User Profile form creating two profiles instead of one
How to populate user profile with django-allauth provider information?
IndexError, list index out of range. Django-allauth signals
django-allauth: how to properly use email_confirmed signal to set user to active
How to populate user profile with django-allauth provider information?
django django-allauth save extra_data from social login in signal

1 个答案:

答案 0 :(得分:0)

您应该使用pre_save保存用户名,并且可以通过更方便的方式进行操作,这里我可以为您提供生成用户名的功能

def unique_username_generator(instance, new_username=None):

    #user_str = get_first_name(instance.name)

    #In your case
     user_str = instance.username 

    if new_username is not None:
        username = new_username

    else:
        user_num = random.randint(1,391020931223)
        username = '{user_str}_{user_num}'.format(user_num=user_num,
                    user_str=user_str)

    klass = instance.__class__

    qs_exists = klass.objects.filter(username=username).exists()
    if qs_exists:
        user_num = random.randint(1,391020931223)
        new_username = '{user_str}_{user_num}'.format(user_num=user_num,
                    user_str=user_str)
        return unique_username_generator(instance,new_username=new_username)

    return username

在模型中对其进行编辑并使用您自己的方法来生成用户名

def username_pre_save_receiver(sender, instance, *args, **kwargs):
    if not instance.username:
        instance.username = unique_username_generator(instance)

pre_save.connect(username_pre_save_receiver, sender=User)