如何在帐户关联后更新用户的"extra_data"?

NT3*_*3RP 5 python django django-socialauth django-1.4

我已成功设法将django-socialauth帐户(在本例中为instagram帐户)与现有用户帐户相关联.我还设置了我的管道以收集其​​他用户详细信息:

def update_social_auth(backend, details, response, social_user, uid, user,
                   *args, **kwargs):

    if getattr(backend, 'name', None) in ('instagram', 'tumblr'):
        social_user.extra_data['username'] = details.get('username')

    social_user.save()
Run Code Online (Sandbox Code Playgroud)

当帐户第一次关联时,这非常有用.但是,如果该帐户已经关联,则该username字段将不会出现在该字段中extra_data.

如何extra_data在关联完成后更新用户?有没有办法django-socialauth在不断开连接和重新连接或使用帐户(例如Instagram的)API的情况下执行此操作?

如果它有帮助,那么这是我目前的管道:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'apps.utils.social.utils.update_social_auth'
)
Run Code Online (Sandbox Code Playgroud)

Gra*_*yne 0

下面是我用来向现有 Django 用户添加“admin”和“staff”选项的代码片段;我不知道django-socialauth这个extra_data领域,但我猜这样的东西可能适用:

 :
userqueryset = User.objects.filter(username=user_name)
if not userqueryset:
    print("User %s does not exist"%user_name, file=sys.stderr)
    return am_errors.AM_USERNOTEXISTS
# Have all the details - now update the user in the Django user database
# see:
#   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
#   https://docs.djangoproject.c om/en/1.7/ref/contrib/auth/#manager-methods
user = userqueryset[0]
user.is_staff     = True
user.is_superuser = True
user.save()
 :
Run Code Online (Sandbox Code Playgroud)

FWIW,我的应用程序正在使用第 3 方身份验证(特别是通过 Google+ 的 atm OpenId Connect),所以我认为这里有一些共同的目标。就我而言,我希望能够向已创建的用户添加 Django 管理员权限。

包含上述代码的完整模块位于github.com/gklyne/annalist/blob/develop/src/annalist_root/annalist_manager/am_createuser.py#L231