【问题标题】:Django social_auth custom user model FacebookBackend explanationDjango social_auth 自定义用户模型 FacebookBackend 解释
【发布时间】:2012-10-17 06:56:07
【问题描述】:

我第一次尝试使用 social_auth (omab),但我发现没有有效的示例如何存储基本的 facebook 用户数据。身份验证有效并且用户创建没有问题,正如 social_auth 文档中所解释的那样,但我还需要存储 genderlocale 。它们都属于基本的facebook用户数据,所以它们一直在facebook响应中。

我正在使用 Django 1.4、Python2.7 和最新的 social_auth。所以我尝试在settings.py文件中使用SOCIAL_AUTH_USER_MODEL = 'myapp.UserProfile',而model.py是:

#!/usr/bin/python
#-*- coding: UTF-8 -*-
from django.db import models
from django.contrib.auth.models import User
from django.db.models import signals
import datetime
from datetime import timedelta
from django.db.models.signals import post_save
from social_auth.signals import pre_update
from social_auth.backends.facebook import FacebookBackend


class CustomUserManager(models.Manager):
    def create_user(self, username, email):
        return self.model._default_manager.create(username=username)

class UserProfile(models.Model):
    gender = models.CharField(max_length=150, blank=True)
    locale = models.CharField(max_length=150, blank=True)
    #social_auth requirements
    username   = models.CharField(max_length=150)
    last_login = models.DateTimeField(blank=True)
    is_active  = models.BooleanField()

    objects = CustomUserManager()

    class Meta:
        verbose_name_plural = 'Profiles'

    def __unicode__(self):
        return self.username

    def get_absolute_url(self):
        return '/profiles/%s/' % self.id

    def facebook_extra_values(sender, user,response, details, **kwargs):     
        profile = user.get_profile()
        current_user = user 
        profile, new = UserProfile.objects.get_or_create(user=current_user)

        profile.gender = response.get('gender')
        profile.locale = response.get('locale')
        profile.save()
        return True

    pre_update.connect(facebook_extra_values, sender=FacebookBackend, weak = False, dispatch_uid = 'facebook_extra_values_user')

在 settings.py 我定义管道

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    #'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.user.create_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'social_auth.backends.pipeline.misc.save_status_to_session',
)

但上面我得到错误AssertionError: ForeignKey(None) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'

我还尝试像以前一样使用AUTH_PROFILE_MODULE = 'myapp.UserProfile' 来扩展user.model,它运行良好,但不明白如何在创建UserProfile 时填充所需的数据。有没有人可以为这个问题放置工作代码?

谢谢

【问题讨论】:

    标签: django django-models django-socialauth django-facebook


    【解决方案1】:

    有多种归档方式,当然,哪种方式更适合您的项目取决于您,以下是可用选项列表:

    1. 定义此设置FACEBOOK_EXTRA_DATA = ('gender', 'locale'),这些值将在UserSocialAuth 实例中可用,要获取它们只需执行user.social_auth.get(provider='facebook').extra_data['gender']['locale']。这是可能的,因为信息在基本用户数据响应中可用。

    2. 使用用户配置文件来存储这些数据(检查django doc 了解它)。然后只需添加一个 pipeline entry 将值存储在您的配置文件实例中。

    3. 创建一个自定义用户模型SOCIAL_AUTH_USER_MODEL = 'myapp.CustomUser',然后再次添加一个自定义管道条目,将值存储在您的用户实例中。

    第 1 号不是 IMO 的最佳解决方案,因为用户可以连接多个 Facebook 帐户,这可能会造成混乱。 2 号适用于 Django 1.4 及更低版本,但从 Django 1.5 开始已弃用,需要考虑到这一点。 IMO 3 号有点乱。

    【讨论】:

    • 感谢您的回答,但如果我尝试第三种解决方案,我不知道该怎么做。自定义用户模型是我的 UserProfile 类,但是如何向它添加管道。
    • 在这种情况下,您有一个配置文件模型,因此您的解决方案是 2 号。检查有关管道的文档,但基本上创建一个类似于 gist.github.com/3906521 的函数,假设您的配置文件模型有性别和语言环境字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2012-06-28
    • 2012-05-05
    • 2014-02-07
    • 2015-03-08
    相关资源
    最近更新 更多