Adding user to group on creation in Django

后端 未结 2 1531
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 01:21

I\'m looking to add a User to a group only if a field of this User is specified as \'True\' once the User is created. Every User that is created would have a \'UserProfile\'

2条回答
  •  情话喂你
    2021-01-03 01:53

    try this:

    def save(self, *args, **kwargs):
        # `save` method of your `User` model
    
        # if user hasnt ID - is creationg operation
        created = self.id is None
        super(YourModel, self).save(*args, **kwargs)
    
        # after save user has ID
        # add user to group only after creating
        if created:
            # adding to group here
    

提交回复
热议问题