I'm trying to store additional information about users that login with facebook to my site, so I created a UserProfile model.
This is how I define the UserProfile:
models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
photo = models.TextField()
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
settings.py
AUTH_PROFILE_MODULE = 'blog.UserProfile'
And, since I'm using python-social-auth for the authentication, I'm implementing a custom pipeline to store the image url of the user in the UserProfile.
from blog.models import UserProfile
def get_profile_picture(
strategy,
user,
response,
details,
is_new=False,
*args,
**kwargs
):
img_url = 'http://graph.facebook.com/%s/picture?type=large' \
% response['id']
profile = UserProfile.objects.get_or_create(user = user)
profile.photo = img_url
profile.save()
But I'm getting following error: 'tuple' object has no attribute 'photo'
I know the UserProfile has the attribute "photo" because this is the definition of that table:
table|blog_userprofile|blog_userprofile|122|CREATE TABLE "blog_userprofile" (
"id" integer NOT NULL PRIMARY KEY,
"user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id"),
"photo" text NOT NULL
)
What it's wrong with my code then?
As the error states, your profile
variable is a tuple, not a UserProfile instance. That is because get_or_create
returns a tuple of (instance, created).