Django: UserProfile with Unique Foreign Key in Django Admin

Posted by lazerscience on Stack Overflow See other posts from Stack Overflow or by lazerscience
Published on 2010-05-11T18:00:59Z Indexed on 2010/05/11 18:04 UTC
Read the original article Hit count: 668

Hi, I have extended Django's User Model using a custom user profile called UserExtension. It is related to User through a unique ForeignKey Relationship, which enables me to edit it in the admin in an inline form! I'm using a signal to create a new profile for every new user:

def create_user_profile(sender, instance, created, **kwargs):  
if created:
    try:  
        profile, created = UserExtension.objects.get_or_create(user=instance)
    except:
        pass  

post_save.connect(create_user_profile, sender=User) 

(as described here for example: http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django) The problem is, that, if I create a new user through the admin, I get an IntegritiyError on saving "column user_id is not unique". It doesnt seem that the signal is called twice, but i guess the admin is trying to save the profile AFTERWARDS? But I need the creation through signal if I create a new user in other parts of the system!

© Stack Overflow or respective owner

Related posts about django

Related posts about admin