def save_model(self, request, obj, form, change): create = False if not obj.id: create = True super(UserAdmin, self).save_model(request, obj, form, change) if create: models.create_user_ldap(obj, form.cleaned_data["password1"])
def save(self): username = self.cleaned_data['username'] password = self.cleaned_data['password'] user = User.objects.create_user(username=username, password=password, email=self.cleaned_data["email"]) # 添加权限 permissions = ( ("change_userprofile", 'account', "userprofile"), # ("delete_accesstoken", 'oauth2', "accesstoken"), ("change_accesstoken", 'oauth2', "accesstoken"), ) for permission in permissions: user.user_permissions.add(Permission.objects.get_by_natural_key(*permission)) user.userprofile = UserProfile( user=user, human_name=self.cleaned_data['human_name'], security_question=self.cleaned_data['security_question'], security_answer=self.cleaned_data['security_answer'], ) #添加ldap账户 create_user_ldap(user, password) user.userprofile.save() return user