我试图通过 Django 中的用户配置文件(1.2.5,Ubuntu natty 中提供的版本)向用户添加一些额外的属性,但是每当我通过管理控制台创建新用户时,都会使用新用户之一包含属性(例如“电话”)时,我收到“列 user_id 不唯一”IntegrityError。
我看到其他人也遇到过这个问题,他们似乎通过在用户配置文件创建信号中添加一个唯一的dispatch_uid来解决这个问题,但这对我来说不起作用:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone = models.CharField(max_length=40,blank=True,null=True)
def create_user_profile(sender, instance, **kwargs):
UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User, dispatch_uid="users-profilecreation-signal")
在 settings.py 中,AUTH_PROFILE_MODULE 设置为指向此类。
有人知道我做错了什么吗?
有两次尝试创建相同的配置文件。一种来自信号,另一种直接来自内嵌的管理表单。
This 问题/答案 https://stackoverflow.com/questions/2813189/django-userprofile-with-unique-foreign-key-in-django-admin更详细地介绍了它和潜在的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)