我已经成功地使用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
进行此操作而不断开和重新连接,或者使用帐户的API (例如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'
)
发布于 2014-11-09 11:07:44
下面是我用来向现有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()
:
FWIW,我的应用程序使用第三方认证(特别是通过OpenId连接Google+),所以我认为这里有一些共同的目标。在我的例子中,我希望能够向已经创建的用户添加Django管理权限。
包含上述代码的完整模块位于createuser.py#L231
https://stackoverflow.com/questions/15956058
复制相似问题