我在django中有一个扩展的UserProfile模型:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
#other things in that profile
还有一个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)
我确保通过在我的__init__.py
中输入信号来注册信号:
import signals
那么应该为每个注册的用户创建一个新的UserProfile,对吧?但事实并非如此。我尝试登录时总是得到“UserProfile匹配查询不存在”错误,这意味着数据库条目不存在。
我应该说我使用django-registration,它提供了user_registered信号。
重要应用程序的结构是,我有一个名为“users”的应用程序,我有:models.py,signals.py,urls.py和views.py(以及其他一些不应该的东西)在这里重要)。 UserProfile类在models.py。中定义。
更新:我将signals.py更改为:
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)
但现在我得到了“IntegrityError”:
“列user_id不是唯一的”
编辑2:
我找到了。看起来我以某种方式注册了两次信号。此处描述了解决方法:http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave
我必须添加一个dispatch_uid,现在我的signals.py看起来像这样并且正在工作:
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")
答案 0 :(得分:30)
您可以在用户上使用post_save实现它:
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)
修改强>
另一种可能的解决方案,经过测试和运行(我在我的网站上使用它):
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)
答案 1 :(得分:6)
您可以在首次访问每个用户时获取要创建的扩展配置文件:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
additional_info_field = models.CharField(max_length=50)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
然后使用
from django.contrib.auth.models import User
user = User.objects.get(pk=1)
user.profile.additional_info_field
参考:http://www.codekoala.com/blog/2009/quick-django-tip-user-profiles/
答案 2 :(得分:6)
这对我有帮助:primary_key = True
class UserProfile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True, related_name="user")
phone = models.CharField(('phone'),max_length=30, blank=False, null=True)
user_building = models.ManyToManyField(Building, blank=True)
added_by = models.ForeignKey(User, blank=True, null=True, related_name="added")
答案 3 :(得分:5)
当您致电profile.setUser()
时,我认为您希望传递instance
而不是sender
作为参数。
从documentation of the user_registered signal,sender
引用User
类; instance
是已注册的实际用户对象。
答案 4 :(得分:1)
根据我的最新研究,创建一个单独的文件,例如singals.py,不起作用。
你最好直接在models.py中将'create_profile'连接到'post_save',否则这段代码将不会被执行,因为它在一个单独的文件中,没有人导入它。
我的最终代码供您参考:
# models.py
# Here goes the definition of class UserProfile.
class UserProfile(models.Model):
...
# Use signal to automatically create user profile on user creation.
# Another implementation:
# def create_user_profile(sender, **kwargs):
# user = kwargs["instance"]
# if kwargs["created"]:
# ...
def create_user_profile(sender, instance, created, **kwargs):
"""
:param sender: Class User.
:param instance: The user instance.
"""
if created:
# Seems the following also works:
# UserProfile.objects.create(user=instance)
# TODO: Which is correct or better?
profile = UserProfile(user=instance)
profile.save()
post_save.connect(create_user_profile,
sender=User,
dispatch_uid="users-profilecreation-signal")
答案 5 :(得分:0)
2018年更新:
这个问题收集了很多观点,也许是时候进行更新了。
这是最新Django的最新版本。
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.conf import settings
from models import UserProfile
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_profile(sender, instance=None, created=False, **kwargs):
if created:
UserProfile.objects.create(user=instance)