我想在django社交应用登录后添加服装领域

时间:2018-10-21 09:38:59

标签: python django facebook login django-socialauth

嗨,我是python / django的PHP开发人员

我正在使用'social-auth-app-django'库使用django创建社交登录名,我按照以下教程进行了实现。

https://simpleisbetterthancomplex.com/tutorial/2016/10/24/how-to-add-social-login-to-django.html

它的工作正常,但我还需要在数据库中添加服装文件,该文件将在不同的表中,但是在创建新用户时将被添加。 我将用户表扩展如下:

from django.contrib.auth.models import User

class NewsCreator(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    CreatorLastLogs= models.CharField(max_length=100)
    CreatorLogs= models.CharField(max_length=100)

并且我想在创建新用户或现有用户登录时向这些字段添加数据。我尝试浏览文档,但找不到与代码扩展/自定义等有关的任何东西。谢谢

1 个答案:

答案 0 :(得分:0)

嗨,我已经找到答案了,所以我为那些稍后会发现该帖子的人发布。

django social提供了扩展代码的管道,我们只需要扩展该管道 为此,请在您的setting.py文件中发布以下列表(此列表中的所有都是默认的管道方法,除了最后一个方法之外,其他方法都将被调用)。

SOCIAL_AUTH_PIPELINE = (
    'social_core.pipeline.social_auth.social_details',
    'social_core.pipeline.social_auth.social_uid',
    'social_core.pipeline.social_auth.auth_allowed',
    'social_core.pipeline.social_auth.social_user',
    'social_core.pipeline.user.get_username',
    'social_core.pipeline.user.create_user',
    'social_core.pipeline.social_auth.associate_user',
    'social_core.pipeline.social_auth.load_extra_data',
    'social_core.pipeline.user.user_details',
    'newsapp.pipeline.save_profile'<-- this is your method
)

在您的应用程序中创建一个名称为pipeline.py的文件,并在列表中提供方法名称,例如列表中的最后一个字符串(newsapp是我的应用程序名称,提供您的应用程序名称)

在pipeline.py文件中

def save_profile(backend, user, response, *args, **kwargs):
    if NewsCreator.objects.filter(user_id=user.id).count() == 0 :
        newsCreator = NewsCreator.objects.create(user=user)
        //your logic for new fields 
        newsCreator.save()

如果您有关于django-social的任何其他查询,可以参考 https://github.com/python-social-auth/social-docs 详细文档