Question

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: 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!

Was it helpful?

Solution

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.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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top