Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django:自动为现有用户创建用户配置文件_Python_Django_Django 1.7 - Fatal编程技术网

Python django:自动为现有用户创建用户配置文件

Python django:自动为现有用户创建用户配置文件,python,django,django-1.7,Python,Django,Django 1.7,今天我在我的项目中添加了一个新的UserProfile模型 class UserProfile(models.Model): user = models.OneToOneField(User) ... def __unicode__(self): return u'Profile of user: %s' % (self.user.username) class Meta: managed = True def create_

今天我在我的项目中添加了一个新的UserProfile模型

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    ...

    def __unicode__(self):
        return u'Profile of user: %s' % (self.user.username)

    class Meta:
        managed = True

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        profile, created = UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)
上面的代码将为每个新创建的用户创建一个用户配置文件

但是如何为每个现有用户自动创建用户配置文件呢


谢谢

为了响应您的代码,我会说在用户的post_init侦听器中也放置一个get_或_create


如果这个“AllFieldsNullisOK”配置文件只是一个快速的例子,我会使用一个中间件将所有没有配置文件的用户重定向到设置页面,要求他们填写额外的数据。(也许你无论如何都想这样做,现实世界中没有人会将新数据添加到他们现有的配置文件中,如果不强制或游戏化的话:)

为了响应你的代码,我会说在用户的post\u init侦听器中也添加一个get\u或\u create


如果这个“AllFieldsNullisOK”配置文件只是一个快速的例子,我会使用一个中间件将所有没有配置文件的用户重定向到设置页面,要求他们填写额外的数据。(也许你无论如何都想这样做,现实世界中没有人会将新数据添加到他们现有的配置文件中,如果不强制或游戏化到其中:)

对于现有用户,它会检查这样的实例是否已经存在,如果不存在,就会创建一个实例

def post_save_create_or_update_profile(sender,**kwargs):
    from user_profiles.utils import create_profile_for_new_user
    if sender==User and kwargs['instance'].is_authenticate():
        profile=None
        if not kwargs['created']:
            try:
                profile=kwargs['instance'].get_profile()
                if len(sync_profile_field(kwargs['instance'],profile)):
                    profile.save()
            execpt ObjectDoesNotExist:
                pass
        if not profile:
            profile=created_profile_for_new_user(kwargs['instance'])
    if not kwargs['created'] and sender==get_user_profile_model():
        kwargs['instance'].user.save()
要连接信号,请使用:

post_save.connect(post_save_create_or_update_profile)

对于现有用户,它检查此类实例是否已经存在,如果不存在,则创建一个实例

def post_save_create_or_update_profile(sender,**kwargs):
    from user_profiles.utils import create_profile_for_new_user
    if sender==User and kwargs['instance'].is_authenticate():
        profile=None
        if not kwargs['created']:
            try:
                profile=kwargs['instance'].get_profile()
                if len(sync_profile_field(kwargs['instance'],profile)):
                    profile.save()
            execpt ObjectDoesNotExist:
                pass
        if not profile:
            profile=created_profile_for_new_user(kwargs['instance'])
    if not kwargs['created'] and sender==get_user_profile_model():
        kwargs['instance'].user.save()
要连接信号,请使用:

post_save.connect(post_save_create_or_update_profile)

您可以循环访问现有用户,并调用
get\u或\u create()


如果愿意,您可以将其放入,或者在shell中运行代码。

您可以循环现有用户,并调用
get\u或\u create()


如果愿意,您可以将其放在一个中,或者在shell中运行代码。

如何在数据迁移中执行此操作?如何在数据迁移中执行此操作?