将附加参数传递给post_save信号 [英] Pass additional parameters to post_save signal

查看:112
本文介绍了将附加参数传递给post_save信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hey i have a user registration form in my django application which collects additional data while a user is trying to register such as address, city, country, phone number etc.

此数据通过 post_save 信号保存在帐户模型类中。用户创建过程如下所示:

This data is saved in the Account model class through post_save signal. The user creation process goes something like this :

# 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方法,以便它填充帐户模型中的字段。

Here the UserRegistration function is called when a user posts a form and under this function i can get the POST data, what i want is to pass that data to create_user_account method so that it fills in the fields in the Account model.

现在我看到在数据库中创建的帐户对象,但所有字段用户字段除外。显然,因为POST变量没有传递给 create_user_account 方法。

Right Now i do see Account Objects created in the database but all the fields except the user field are empty. Obviously, because the POST variables are not being passed to the create_user_account method.

推荐答案

我想象你的情况可能是:

I imagine your case could be:

# 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)定义特定实例的信号处理程序所需的数据。

I hate to do this, and I imagine it can breaks in horrible ways, and is hard to debug sometimes, also there is no a strict way to force the data needed for the handler, one could define a signal_data(data, signal, instance) to define the data needed for the signal handler for a particular instance.

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

A nice option that I haven't tried is to use methods of the instance as signal's handlers and maybe we can use a more structured way to pass the data.

再见。

这篇关于将附加参数传递给post_save信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆