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

28👍

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

Leave a comment