【问题标题】:How to update a user's `extra_data` after they have been associated with account?与帐户关联后如何更新用户的`extra_data`?
【发布时间】:2013-04-04 01:51:44
【问题描述】:

我已成功使用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()

这在首次关联帐户时非常有用。但是,如果帐户已经关联,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'
)

【问题讨论】:

    标签: python django django-socialauth django-1.4


    【解决方案1】:

    这是我用来向现有 Django 用户添加“admin”和“staff”选项的代码的 sn-p;我不知道django-socialauthextra_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()
     :
    

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

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

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2012-11-27
      • 2018-03-30
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多