Django social_auth自定义用户模型FacebookBackend解释

时间:2012-10-17 06:56:07

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

我第一次尝试使用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时填充所需的数据。有没有人可以为这个问题安排工作代码?

由于

1 个答案:

答案 0 :(得分:2)

有几种方法可以归档它,当然,最适合您项目的方法取决于您,这里有一个可用选项列表:

  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',然后再次添加一个自定义管道条目,用于存储用户实例中的值。

  4. 1号不是IMO的最佳解决方案,因为用户可以连接多个Facebook帐户并且可能造成混乱。 2号对Django 1.4及更低版本有好处,但从Django 1.5开始不推荐使用它,需要注意的事项。 3号是IMO有点乱。

相关问题