创建扩展用户配置文件 [英] Creating a extended user profile

查看:108
本文介绍了创建扩展用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中有一个扩展的UserProfile模型:

  class UserProfile(models.Model):
user = models.ForeignKey(User,unique = True)
#该配置文件中的其他内容

一个signals.py:

  from registration.signals import user_registered 
from models import UserProfile
from django。导入用户

def createUserProfile(sender,instance,** kwargs):
profile = users.models.UserProfile()
profile.setUser(sender)
profile.save()

user_registered.connect(createUserProfile,sender = User)

我确保信号通过在我的 __ init __。py 中得到注册:

 导入信号

所以应该为每个人创建一个新的UserProfile注册用户,对吧?但是没有。当我尝试登录时,我总是得到UserProfile匹配查询不存在错误,这意味着数据库条目不存在。



我应该说我使用django-registration,它提供了user_registered信号。



这个重要的应用程序的结构是,我有一个名为users的应用程序,我有: models.py,signals.py,urls.py和views.py(以及其他一些在这里不重要的事情)。 UserProfile类在models.py中定义。



更新:我将signals.py更改为:


来自django.db.models.signals的

 导入post_save 
从模型import UserProfile
from django.contrib.auth.models import User

def create_profile(sender,** kw):
user = kw [instance]
如果kw [created]:
profile = UserProfile()
profile.user = user
profile.save()

post_save.connect(create_profile,sender =用户)

但现在我得到一个IntegrityError:


列user_id不唯一


编辑2:



我找到了。看起来不知何故,我注册了两次信号。以下介绍了解决方法: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave



我不得不添加一个dispatch_uid,现在我的signals.py看起来像这样,正在工作:



从django.db.models.signals导入post_save
从django.contrib.auth.models导入用户
从模型导入UserProfile
从django .db导入模型

def create_profile(sender,** kw):
user = kw [instance]
如果kw [created]:
profile = UserProfile(user = user)
profile.save()

post_save.connect(create_profile,sender = User,dispatch_uid =users-profilecreation-signal)


解决方案

您可以使用post_save在用户上实现:

$ b来自django.db.models.signals imp的
$ b

  ort post_save 
from models import UserProfile
from django.contrib.auth.models import User

def create_profile(sender,** kwargs):
user = kwargs [ instance]
如果kwargs [created]:
profile = users.models.UserProfile()
profile.setUser(sender)
profile.save()

post_save.connect(create_profile,sender = User)

编辑:

另一种可能的解决方案,经测试和工作(我在我的网站上使用它):

 code>从django.db导入模型
从django.contrib.auth.models import用户
从django.db.models.signals import post_save
def create_profile(sender,** kwargs)
user = kwargs [instance]
如果kwargs [created]:
up = UserProfile(user = user,stuff = 1,thing = 2)
up.save()
post_save.connect(create_profile,sender = User)


I have an extended UserProfile model in django:

class UserProfile(models.Model):
  user = models.ForeignKey(User, unique=True)
  #other things in that profile

And a signals.py:

from registration.signals import user_registered
from models import UserProfile
from django.contrib.auth.models import User

def createUserProfile(sender, instance, **kwargs):
  profile = users.models.UserProfile()
  profile.setUser(sender)
  profile.save()

user_registered.connect(createUserProfile, sender=User)

I make sure the signal gets registered by having this in my __init__.py:

import signals

So that should create me a new UserProfile for every user that registers, right? But it doesn't. I always get "UserProfile matching query does not exist" errors when I try to log in, which means that the database entry isn't there.

I should say that I use django-registration, which provides the user_registered signal.

The structure of the important apps for this is, that I have one application called "users", there I have: models.py, signals.py, urls.py and views.py (and some other things which shouldn't matter here). The UserProfile class is defined in models.py.

Update: I changed the signals.py to:

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

def create_profile(sender, **kw):
    user = kw["instance"]
    if kw["created"]:
        profile = UserProfile()
        profile.user = user
        profile.save()

post_save.connect(create_profile, sender=User)

But now I get a "IntegrityError":

"column user_id is not unique"

Edit 2:

I found it. Looks like somehow I registred the signal twice. The workaround for this is described here: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave

I had to add a dispatch_uid, now my signals.py looks like this and is working:

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

def create_profile(sender, **kw):
    user = kw["instance"]
    if kw["created"]:
        profile = UserProfile(user=user)
        profile.save()

post_save.connect(create_profile, sender=User, dispatch_uid="users-profilecreation-signal")

解决方案

You can implement it using post_save on the user:

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

def create_profile(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        profile = users.models.UserProfile()
        profile.setUser(sender)
        profile.save()

post_save.connect(create_profile, sender=User)

Edit:
Another possible solution, which is tested and works (I'm using it on my site):

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
def create_profile(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        up = UserProfile(user=user, stuff=1, thing=2)
        up.save()
post_save.connect(create_profile, sender=User)

这篇关于创建扩展用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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