【问题标题】:How would i use signals to create a new "UserProfile" object when a "User" object is created in django?当在 django 中创建“用户”对象时,我将如何使用信号来创建新的“用户配置文件”对象?
【发布时间】:2011-06-06 12:40:11
【问题描述】:

嘿,我正在使用 django 的 contrib.auth 系统,它显然允许我创建用户对象,我也在使用配置文件模块。这是通过 AUTH_PROFILE_MODULE 加载的。

使用信号在创建用户时如何创建新的 UserProfile 对象?

谢谢

【问题讨论】:

标签: django signals profile


【解决方案1】:

我正在帐户中创建一个新条目作为我的用户配置文件:

from django.db.models.signals import post_save
from django.contrib.auth.models import User


from wizard.models import Account

 def make_account(sender, **kwargs):

    if 'created' not in kwargs or not kwargs['created']:
        return

    user = kwargs["instance"]
    account = Account(user=user, name="Account for %s" % user.username)

    account.save()

 post_save.connect(make_account, sender=User, weak=False)

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    相关资源
    最近更新 更多