3

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 at this class, in settings.py.

Anyone know what I'm doing wrong?

paul88888
  • 336
  • 5
  • 13

2 Answers2

5

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.

Community
  • 1
  • 1
AndrewS
  • 1,666
  • 11
  • 15
0

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)
Mark Mikofski
  • 19,398
  • 2
  • 57
  • 90
AndrewS
  • 1,666
  • 11
  • 15
  • Hmm, getting the same error with that too. I'm afraid I'm at a loss to know whether that means it's a race condition or not... Cheers. – paul88888 Aug 17 '11 at 01:38