python - 将附加参数传递给 post_save 信号

标签 python django

我的 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 方法。

最佳答案

我所做的是为实例设置一些“_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) 定义特定实例的信号处理程序所需的数据。

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

再见。

关于python - 将附加参数传递给 post_save 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11179380/

相关文章:

python - 模板不存在于/admin/MY_APP/my_model/reversion/change_list.html

python - Cron 作业错误 : accessing Datastore and emailing

Python字典替换值

python - 保存 Unicode 时 MySql 出现 "Incorrect string value"错误

python - 表单提交而不是 POST 是 Django 中的值

python - 获取 django 对象的一对多设置属性的名称

Python - Pandas 数据操作来计算基尼系数

python - Django queryset iterator() 无法按预期工作

javascript - Django/Jquery/Javascript - 如何使用 url 参数预填充表单(自动填充表单)

django - 如何对名称相似的项目进行分组?