Django: UserProfile with Unique Foreign Key in Django Admin

我们两清 提交于 2019-11-27 02:18:48

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/2813189/django-userprofile-with-unique-foreign-key-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!