22

我的 Django 应用程序中有一个用户注册表单,它在用户尝试注册时收集其他数据,例如地址、城市、国家/地区、电话号码等。

此数据通过post_save信号保存在 Account 模型类中。用户创建过程是这样的:

# Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
    if created:
      models.Account.objects.create(user=instance)

# Create User / User Registration
def UserRegistration(request):
    if request.method == 'POST':
        username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
        # CREATE USER
        newuser = User.objects.create_user(username=username, email=request.POST['email'], password=request.POST['pw'])
        newuser.first_name = request.POST['fn'].capitalize()
        newuser.last_name = request.POST['ln'].capitalize()
        newuser.save()
    return HttpResponse(username)

#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User)

在这里,UserRegistration当用户发布表单时调用该函数,在此函数下,我可以获得 POST 数据,我想要将该数据传递给create_user_account方法,以便它填充Account模型中的字段。

现在,我确实看到Account在数据库中创建了对象,但是除了用户字段之外的所有字段都是空的。显然,因为 POST 变量没有被传递给create_user_account方法。

4

3 回答 3

22

我所做的是为实例设置一些“_attrs”,然后在信号处理程序中使用它们。

我想你的情况可能是:

# Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
    if created:
        attrs_needed = ['_language', '_field', '_otherfield']
        if all(hasattr(instance, attr) for attr in attr_needed):
            models.Account.objects.create(
                user=instance, 
                language=instance._language, 
                field=instance._field,
                otherfield=instance._otherfield)

# Create User / User Registration
def UserRegistration(request):
  if request.method == 'POST':
    username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
    # CREATE USER
    newuser = User.objects.create_user(
        username=username, email=request.POST['email'],
        password=request.POST['pw'])
    newuser.first_name = request.POST['fn'].capitalize()
    newuser.last_name = request.POST['ln'].capitalize()

    # Set some extra attrs to the instance to be used in the handler.
    newuser._language = request.POST['language']
    newuser._field = request.POST['field']
    newuser._otherfield = request.POST['otherfield']
    newuser.save()


  return HttpResponse(username)

#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User)

我讨厌这样做,我想它可能会以可怕的方式中断,有时很难调试,也没有严格的方法来强制处理程序所需的数据,可以定义一个signal_data(data, signal, instance)来定义所需的数据特定实例的信号处理程序。

我没有尝试过的一个不错的选择是使用实例的方法作为信号的处理程序,也许我们可以使用更结构化的方式来传递数据。

再见。

于 2012-07-09T01:23:17.147 回答
2

两者User.objects.create_userUser.objects.create立即触发post_save处理程序,因为saveUserManager create_user. 所以我无法想象豪尔赫的答案是如何起作用的(至少如果你希望保存只被触发一次 - 你为什么要触发它两次???)。你想要做的是研究什么create_user,剖析它,这样你就可以真正控制何时save调用:

# Function to Create user Account/Profile
def create_user_account(sender, instance, created, **kwargs):
    if created:
        attrs_needed = ['_language', '_field', '_otherfield']
        if all(hasattr(instance, attr) for attr in attr_needed):
            models.Account.objects.create(
                user=instance, 
                language=instance._language, 
                field=instance._field,
                otherfield=instance._otherfield)

# Create User / User Registration
def UserRegistration(request):
  if request.method == 'POST':
    username = request.POST['fn'].capitalize() + ' ' + request.POST['ln'].capitalize()
    # CREATE USER
    newuser = User(
        username=username,
        email=request.POST['email'],
        first_name=request.POST['fn'].capitalize()
        last_name = request.POST['ln'].capitalize()
    )
    newuser.set_password(request.POST['pw'])

    # Set some extra attrs to the instance to be used in the handler.
    newuser._language = request.POST['language']
    newuser._field = request.POST['field']
    newuser._otherfield = request.POST['otherfield']
    newuser.save()  # Now this will be really the first save which is called

  return HttpResponse(username)

#Post Save handler to create user Account/Profile
post_save.connect(create_user_account, sender=User, weak=False)

请注意,weak=False当我连接处理程序时,我也会使用。

于 2017-12-09T02:03:07.547 回答
0

我发现receiver(post_save, sender=User)当我们使用create_userandcreate方法时会立即调用,所以,我们需要先初始化对象,然后填充额外的字段。

于 2022-01-15T07:42:04.453 回答