我正在使用Django社交身份验证并尝试集成自定义用户模型。
class MyUserManager(BaseUserManager):
def create_user(self, userid, accesstoken, firstname, lastname, password=None):
"""
Creates and saves a User with the given userid, accesstoken,
firstname, lastname and password.
"""
if not userid:
raise ValueError('Users must have userid')
user = self.model(
userid=userid,
accesstoken=accesstoken,
firstname=firstname,
lastname=lastname
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, userid, accesstoken, firstname, lastname, password):
"""
Creates and saves a User with the given userid, accesstoken,
firstname, lastname and password.
"""
user = self.create_user(userid,
password=password,
accesstoken=accesstoken,
firstname=firstname,
lastname=lastname
)
user.is_admin = True
user.save(using=self._db)
return user
class CustomUser(AbstractBaseUser):
userid = models.BigIntegerField(primary_key=True,unique=True)
email = models.EmailField(
verbose_name='email address',
max_length=255,
# unique=True,
db_index=True,
)
accesstoken = models.CharField(max_length=500L, db_column='accessToken') # Field name made lowercase.
expiresin = models.IntegerField(db_column='expiresIn') # Field name made lowercase.
firstname = models.CharField(max_length=100L)
lastname = models.CharField(max_length=100L)
fullname = models.CharField(max_length=200L)
birthday = models.CharField(max_length=100L)
time = models.DateTimeField()
tourshown = models.IntegerField(null=True, blank=True)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'userid'
REQUIRED_FIELDS=['accesstoken','firstname','lastname']
objects = MyUserManager()
def get_full_name(self):
# The user is identified by their email address
return self.firstname + ' ' + self.lastname
def get_short_name(self):
# The user is identified by their email address
return self.firstname
def __unicode__(self):
return self.firstname + ' ' + self.lastname
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
class Meta:
db_table = 'customUser'
和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.get_username',
'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',
'mobile.pipeline.populateuserinfo',)
SOCIAL_AUTH_USER_MODEL = 'mobile.CustomUser'
这里populateuserinfo是我写的一个管道方法。
现在,当我尝试使用Facebook登录时,我被重定向到登录错误网址。没有任何有用的错误消息。这里有什么明显的错误需要修复,或者有没有办法我可以调试它并弄清楚出了什么问题。
PS:错误不是由于我的管道方法,因为我被重定向到登录错误,即使我不将它作为管道方法保留。
编辑:我尝试对工作用户模型进行增量更改,确保为每个用户提供电子邮件并保留first_name和last_name字段名称。这似乎帮助我让它运行,我能够验证代码是否通过指定的管道。但是,在通过管道后我仍然被重定向到登录错误页面,我无法验证原因。如果有人能告诉我如何调试它,我将不胜感激。