처음으로 social_auth (omab)을 사용하려고하는데 기본적인 페이스 북 사용자 데이터를 저장하는 방법이 없다는 것을 알게되었습니다. 인증은 작동하며 사용자는 social_auth 문서에 설명 된대로 문제없이 작성되지만 gender
및 locale
도 저장해야합니다. 둘 다 기본 페이 스북 사용자 데이터에 속하므로 항상 페이스 북 응답에 있습니다.Django social_auth 맞춤형 사용자 모델 FacebookBackend 설명
저는 Django 1.4, Python2.7 및 최신 social_auth를 사용하고 있습니다. 그래서 settings.py 파일 model.py에 SOCIAL_AUTH_USER_MODEL = 'myapp.UserProfile'
를 사용하려고 한 것은 : 내가 파이프 라인을
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',
)
를 정의 해요 settings.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')
하지만 오류 AssertionError: ForeignKey(None) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'
또한 잘 작동하지만 UserProfile이 생성 될 때 필요한 데이터를 채우는 방법을 이해하지 못하는 user.model을 확장하기 전에 내가했던 것처럼 AUTH_PROFILE_MODULE = 'myapp.UserProfile'
을 대신 사용하려고했습니다. 누구든지이 문제에 대한 작업 코드를 배치 할 수 있습니까?
감사
해답을 주셔서 감사합니다. 그러나 제 3의 해결책을 시도해도이 작업을 수행하는 방법을 모르겠습니다. 사용자 정의 사용자 모델은 내 UserProfile 클래스이지만 파이프 라인을 추가하는 방법입니다. – Goran
이 경우에는 프로파일 모델이 있으므로 솔루션은 2 번입니다. 파이프 라인에 대한 문서를 확인하십시오. 기본적으로이 프로파일과 유사한 함수를 만듭니다. https://gist.github.com/3906521, 이는 사용자의 프로파일 모델 성 및 로케일 필드가 있습니다. – omab
나는 아주 큰 omab, 예상대로 작동 감사합니다 말할 수 있습니다. – Goran