1

Facebook でログインしたユーザーに関する追加情報を自分のサイトに保存しようとしているので、UserProfile モデルを作成しました。

これは、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)

設定.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()

しかし、次のエラーが表示されます: 'tuple' object has no attribute 'photo'

これがそのテーブルの定義であるため、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
)

私のコードの何が問題なのですか?

4

1 に答える 1

5

エラーが示すように、profile変数は UserProfile インスタンスではなくタプルです。これはget_or_create、(instance, created) のタプルを返すためです。

于 2014-02-16T20:26:27.263 に答える