我正在尝试存储有关使用 facebook 登录到我的网站的用户的其他信息,因此我创建了一个 UserProfile 模型。
这就是我定义用户配置文件的方式:
模型.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)
设置.py
AUTH_PROFILE_MODULE = 'blog.UserProfile'
而且,由于我使用 python-social-auth 进行身份验证,因此我正在实现一个自定义管道来将用户的图像 url 存储在 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()
但我收到以下错误:“元组”对象没有属性“照片”
我知道 UserProfile 具有属性“照片”,因为这是该表的定义:
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
)
那我的代码有什么问题?