Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the user's facebook id with django-allauth?

I want to display a picture of the current_user in my template. How can I access the user's facebook id when I'm using django-allauth?

like image 308
fceruti Avatar asked Aug 31 '12 20:08

fceruti


1 Answers

For each user that is signed up via a social account a SocialAccount instance is available. This model has a foreign key to User. Note that a user can connect multiple social networking accounts to his local account, so in practice there may be more than one SocialAccount instances available.

How you want to deal with this is project dependent. I can imagine one would want to copy the profile image locally, or perhaps you would want to give preference to the Google+ profile picture above the Facebook one, and so on.

The SocialAccount model has some helper methods that give you access to account basics such as the profile picture. All in all, this is a quick & dirty way to access first available profile picture:

{{user.socialaccount_set.all.0.get_avatar_url}}

The ID is also available:

{{user.socialaccount_set.all.0.uid}}

See also: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/models.py#L72

like image 158
pennersr Avatar answered Nov 14 '22 05:11

pennersr