2

From this post: How can I get the user's facebook id with django-allauth? there is a simple way of accessing extra information from a user's social account within a template (e.g. looking at the entries in {{ user.socialaccount_set.all.0.extra_data }}).

I would like to grab this information in a view, rather than the template, ideally looping through all connected social accounts, and grabbing the more desirable information from each, and then passing only that information to the template.

Does anyone know how to access socialaccount_set.all from within a view?

4

3 回答 3

4

我知道这个问题发布已经一年多了,但我在任何地方都找不到答案。

这就是我最终的做法,以防将来对任何人有所帮助:

#views.py

user.socialaccount_set.all()[0].extra_data
于 2014-09-28T08:20:36.200 回答
2

这是在视图中访问为用户存储的任何信息的替代方法:

from allauth.socialaccount.models import SocialAccount

social_info = SocialAccount.objects.filter(user=request.user)
fb_id = SocialAccount.objects.filter(user=request.user, provider='facebook')[0].uid
于 2017-09-05T22:01:33.550 回答
0

继续上面的回答,或许可以尝试使用:

SocialAccount.objects.filter(user=request.user)[0].extra_data['email']

它将获取电子邮件帐户的额外数据。希望这有帮助:)

于 2018-12-14T09:20:59.267 回答