Django的。 Python社会认证。在管道末端创建轮廓 [英] Django. Python social auth. create profiles at the end of pipeline
问题描述
我想在auth流水线末尾添加一个函数,该函数用于检查该用户是否有Profiles表,如果不存在,将创建一个表。
Profiles模型是一个表,其中存储有关用户的一些额外信息:
I want to add a function at the end of the auth pipeline, the function is meant to check if there is a "Profiles" table for that user, if there isn't it will create a table. The Profiles model is a table where I store some extra information about the user:
class Profiles(models.Model):
user = models.OneToOneField(User, unique=True, null=True)
description = models.CharField(max_length=250, blank=True, null=True)
points = models.SmallIntegerField(default=0)
posts_number = models.SmallIntegerField(default=0)
每个用户必须具有个人档案表。所以,我在管道末端添加了一个函数:
Each user must have a Profiles table. So, I added a function at the end of the pipeline:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'social.pipeline.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'app.utils.create_profile' #Custom pipeline
)
#utils.py
def create_profile(strategy, details, response, user, *args, **kwargs):
username = kwargs['details']['username']
user_object = User.objects.get(username=username)
if Profiles.ojects.filter(user=user_object).exists():
pass
else:
new_profile = Profiles(user=user_object)
new_profile.save()
return kwargs
我收到错误:
KeyError at /complete/facebook/
'details'
...
utils.py in create_profile
username = kwargs['details']['username']
我是蟒蛇社交认证的新手,看起来我缺少一些明显的东西。任何帮助将不胜感激。
I'm new to python social auth, and it looks that I'm missing something obvious. Any help will be appreciated.
推荐答案
好的,所以我会回答我自己的问题,以防万一它对未来。我不是专家,但这里是:
Ok so I'll answer my own question just in case it is useful for someone in the future. I'm no expert but here it is:
我在追踪这个教程,因为他是
I was following this tutorial, and becouse he does
email = kwargs['details']['email']
我以为我可以做
username = kwargs['details']['username']
但它没有起作用,它给了我一个KeyError。
But it didn't work, it gave my a KeyError.
然后我试过:
username = details['username']
它的工作。但是我有一个新的问题,详细信息的用户名是u'Firstname Lastname,当我试图获取User对象
And it worked. But I had a new problem, the username from the details dict was something like u'Firstname Lastname' and when I tried to get the User object
user_object = User.objects.get(username=username)
没有找到,因为用户模型中的用户名是u'FirstnameLastname'(没有空格)。
It was not found, becouse the username in the User model was u'FirstnameLastname' (without the space).
最后我再次阅读了文档,我发现我可以简单地使用用户对象直接,它被传递给函数为user:
Finally I read the docs again and I found out that I could simply use the user object directly, it gets passed to the function as "user":
def create_profile(strategy, details, response, user, *args, **kwargs):
if Profiles.objects.filter(usuario=user).exists():
pass
else:
new_profile = Profiles(user=user)
new_profile.save()
return kwargs
这篇关于Django的。 Python社会认证。在管道末端创建轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!