views:

383

answers:

1

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!

+2  A: 

It is normal that django will create the admin instance afterwards, as the saving consists always of something like this:

  1. Create User object
  2. Create Profile object (can't be before because it points to a user).

When saving the User object the django ORM cannot know the create profile object will come after it so it will not delay the post_save signal in any way (doesn't even make sense).

The best way to handle this (imho) if you want to keep the post_save signal, is to override the save method of UserExtension to something like this:

def save(self, *args, **kwargs):
    try:
        existing = UserExtension.objects.all().get(user=self.user)
        self.id = existing.id #force update instead of insert
    except UserExtension.DoesNotExist:
        pass 
    models.Model.save(self, *args, **kwargs)

Note that this does force every insert that points to the same user as an existing object to become an update, this can be unexpected behaviour for other parts of code.

KillianDS
Overriding the model's default save method seems like a good and easy idea to me, will have to check if it works for me in all use cases!I think "existing = UserExtension.objects.all().get(user=self.user)" can be "existing = UserExtension.objects.get(user=self.user)", or did you have anything special in mind with that? :)Thanks!
lazerscience
Nothing special, you don't need the `.all()` as far as I know. I'm not sure why I added it
KillianDS