开发者

column user_id is not unique, with Django UserProfiles

开发者 https://www.devze.com 2023-03-28 09:02 出处:网络
I\'m trying to add some extra attributes to a user by way of a User Profile in Django (1.2.5, the version supplied in Ubuntu natty), but whenever I create a new user through the admin console, with on

I'm trying to add some extra attributes to a user by way of a User Profile in Django (1.2.5, the version supplied in Ubuntu natty), but whenever I create a new user through the admin console, with one of the new attributes included (eg, 'phone'), I get a "column user_id is not unique" IntegrityError.

I see that other people have had this problem, and they seem to have solved it by adding a unique dispatch_uid to the userprofile creation signal, but that's not working for me:

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")

AUTH_PROFILE_MODULE is set to point开发者_StackOverflow社区 at this class, in settings.py.

Anyone know what I'm doing wrong?


There are two attempts to create the same profile. One from the signal and one directly from the inlined admin form.

This question/answer covers it and a potential solution in more detail.


Perhaps check to see if this is a signal for creating a user. You may be seeing a race condition with a number of callbacks.

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

精彩评论

暂无评论...
验证码 换一张
取 消